Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 22nd, 2012, 11:00 AM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Question XSLT Grouping problem

XSLT 1.0 & 2.0(SaxonHE)

Thank you in advance. I am writing the whole day trying to solve the problem, but still I cannot get the results which I want. I tried XSLT 1.0, and XSLT 2.0, no luck. At the end I posted some codes to deal with the some parts of the input which you could not worried about.

There are two input cases(only one case will show up at one time), the different is that when there are several sections, it will have "--------------------------------------" as the beginning of each section; when there is only on section, it will not have "--------------------------------------".

Input 1 XML, this XML has several section, and we should grouping the things between "--------------------------------------", we need to change the "--------------------------------------" to "SECTION" after <MTX><MXT02>ATN: 10000001</MTX02></MTX>, and grab the things between "--------------------------------------":
Code:
<ISA>
  <ISA01>01</ISA01>
  <ISA02>02</ISA02>
  <ISA03>03</ISA03>
  <GS>
    <GS01>PR</GS01>
    <GS02>FTRPO</GS02>
    <ST>
      <ST01>855</ST01>
      <ST02>0001</ST02>
      <BAK>
        <BAK01>11</BAK01>
        <BAK02>AT</BAK02>
        <BAK03>117624</BAK03>
        <BAK04>20120321</BAK04>
      </BAK>
      <DTM>
        <DTM01>097</DTM01>
        <DTM02>20120321</DTM02>
        <DTM03>162329</DTM03>
      </DTM>
      <SI>
        <SI01>TI</SI01>
        <SI02>IQ</SI02>
        <SI03>E</SI03>
      </SI>
      <N1>
        <N101>BY</N101>
        <N103>25</N103>
        <N104>180A</N104>
      </N1>
      <PO1>
        <PO101>1</PO101>
        <PO102>1</PO102>
        <PO103>EA</PO103>
        <PO106>A6</PO106>
        <PO107>CSA</PO107>
      </PO1>
      <N9>
        <N901>ME</N901>
        <N902>MLT</N902>
      </N9>
      <MTX>
        <MTX02>ATN: 10000001</MTX02>
      </MTX>
      <MTX>
        <MTX02>--------------------------------------</MTX02>
      <MTX>
      <MTX>
        <MTX02>WTN:11111111</MTX02>
      <MTX>
      <MTX>
        <MTX02>--LST</MTX02>
      </MTX>
      <MTX>
        <MTX02>SA 1040 BROADWAY , NEEDLES , CA 92363</MTX02>
      </MTX>
      <MTX>
        <MTX02>NP TSR Inc</MTX02>
      </MTX>
      <MTX>
        <MTX02>LA1 1040 E Broadway</MTX02>
      </MTX>
      <MTX>
        <MTX02>--BILL</MTX02>
      </MTX>
      <MTX>
        <MTX02>USI .............. 210053119900</MTX02>
      </MTX>
      <MTX>
        <MTX02>Address Line 1 ... ATTN DEPT 13</MTX02>
      </MTX>
      <MTX>
        <MTX02>Address Line 2 ... 2005 HUMMEL AVE</MTX02>
      </MTX>
      <MTX>
        <MTX02>--S&amp;E</MTX02>
      </MTX>
      <MTX>
        <MTX02>S&amp;E SEQ Description Rat</MTX02>
      </MTX>
      <MTX>
        <MTX02>B0ALL 1 I Block All Svc Providers .00</MTX02>
      </MTX>
      <MTX>
        <MTX02>B1LCP 1 Bus 1pty Loc Clg Pln-Meas 32.00</MTX02>
      </MTX>
      <MTX>
        <MTX02>CP699 1 Local Calling Plan .00</MTX02>
      </MTX>
      <MTX>
        <MTX02>--LPIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>Preferred Carrier ..........+0PIC NO PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>--PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>Preferred Carrier ..........+0PIC NO PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>--PROV</MTX02>
      </MTX>
      <MTX>
        <MTX02>Intercept Removal Date: 0/00/00</MTX02>
      </MTX>
      <MTX>
        <MTX02>Last SO to Update:063384164</MTX02>
      </MTX>
      <MTX>
        <MTX02>--------------------------------------</MTX02>
      <MTX>
      <MTX>
        <MTX02>WTN:22222222</MTX02>
      <MTX>
      <MTX>
        <MTX02>--LST</MTX02>
      </MTX>
      <MTX>
        <MTX02>22SA 1040 BROADWAY , NEEDLES , CA 92363</MTX02>
      </MTX>
      <MTX>
        <MTX02>22NP TSR Inc</MTX02>
      </MTX>
      <MTX>
        <MTX02>22LA1 1040 E Broadway</MTX02>
      </MTX>
      <MTX>
        <MTX02>--BILL</MTX02>
      </MTX>
      <MTX>
        <MTX02>22USI .............. 210053119900</MTX02>
      </MTX>
      <MTX>
        <MTX02>22Address Line 1 ... ATTN DEPT 13</MTX02>
      </MTX>
      <MTX>
        <MTX02>22Address Line 2 ... 2005 HUMMEL AVE</MTX02>
      </MTX>
      <MTX>
        <MTX02>--S&amp;E</MTX02>
      </MTX>
      <MTX>
        <MTX02>S&amp;E SEQ Description Rat</MTX02>
      </MTX>
      <MTX>
        <MTX02>22B0ALL 1 I Block All Svc Providers .00</MTX02>
      </MTX>
      <MTX>
        <MTX02>22B1LCP 1 Bus 1pty Loc Clg Pln-Meas 32.00</MTX02>
      </MTX>
      <MTX>
        <MTX02>22CP699 1 Local Calling Plan .00</MTX02>
      </MTX>
      <MTX>
        <MTX02>--LPIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>22Preferred Carrier ..........+0PIC NO PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>--PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>22Preferred Carrier ..........+0PIC NO PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>--PROV</MTX02>
      </MTX>
      <MTX>
        <MTX02>22Intercept Removal Date: 0/00/00</MTX02>
      </MTX>
      <MTX>
        <MTX02>22Last SO to Update:063384164</MTX02>
      </MTX>
      <CTT>
        <CTT01>1</CTT01>
      </CTT>
    </ST>
  </GS>
</ISA>
So the output should be:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <TXNUM>117624</TXNUM>
  <DTSENT>20120321162329</DTSENT>
  <PRESPC>000</PRESPC>
  <PRESPD>Completed</PRESPD>
  <unparsedCSR>
    <ATN>7603266875</ATN>
    <SECTION>
      <MTX>WTN:11111111</MTX>
      <MTX>--LST</MTX>
      <MTX>SA 1040 BROADWAY , NEEDLES , CA 92363</MTX>
      <MTX>NP TSR Inc</MTX>
      <MTX>LA1 1040 E Broadway</MTX>
      <MTX>--BILL</MTX>
      <MTX>USI .............. 210053119900</MTX>
      <MTX>Address Line 1 ... ATTN DEPT 13</MTX>
      <MTX>Address Line 2 ... 2005 HUMMEL AVE</MTX>
      <MTX>--S&amp;E</MTX>
      <MTX>S&amp;E SEQ Description Rat</MTX>
      <MTX>B0ALL 1 I Block All Svc Providers .00</MTX>
      <MTX>B1LCP 1 Bus 1pty Loc Clg Pln-Meas 32.00</MTX>
      <MTX>CP699 1 Local Calling Plan .00</MTX>
      <MTX>--LPIC</MTX>
      <MTX>Preferred Carrier ..........+0PIC NO PIC</MTX>
      <MTX>--PIC</MTX>
      <MTX>Preferred Carrier ..........+0PIC NO PIC</MTX>
      <MTX>--PROV</MTX>
      <MTX>Intercept Removal Date: 0/00/00</MTX>
      <MTX>Last SO to Update:063384164</MTX>
    </SECTION>
    <SECTION>
      <MTX>WTN:22222222</MTX>
      <MTX>--LST</MTX>
      <MTX>22SA 1040 BROADWAY , NEEDLES , CA 92363</MTX>
      <MTX>22NP TSR Inc</MTX>
      <MTX>22LA1 1040 E Broadway</MTX>
      <MTX>--BILL</MTX>
      <MTX>22USI .............. 210053119900</MTX>
      <MTX>22Address Line 1 ... ATTN DEPT 13</MTX>
      <MTX>22Address Line 2 ... 2005 HUMMEL AVE</MTX>
      <MTX>--S&amp;E</MTX>
      <MTX>S&amp;E SEQ Description Rat</MTX>
      <MTX>22B0ALL 1 I Block All Svc Providers .00</MTX>
      <MTX>22B1LCP 1 Bus 1pty Loc Clg Pln-Meas 32.00</MTX>
      <MTX>22CP699 1 Local Calling Plan .00</MTX>
      <MTX>--LPIC</MTX>
      <MTX>22Preferred Carrier ..........+0PIC NO PIC</MTX>
      <MTX>--PIC</MTX>
      <MTX>22Preferred Carrier ..........+0PIC NO PIC</MTX>
      <MTX>--PROV</MTX>
      <MTX>22Intercept Removal Date: 0/00/00</MTX>
      <MTX>22Last SO to Update:063384164</MTX>
    </SECTION>

Input 2 XML, this XML has only one section, we need to create a new node as "SECTION" and grab all the node after <MTX><MXT02>ATN: 10000001</MTX02></MTX>
Code:
<ISA>
  <ISA01>01</ISA01>
  <ISA02>02</ISA02>
  <ISA03>03</ISA03>
  <GS>
    <GS01>PR</GS01>
    <GS02>FTRPO</GS02>
    <ST>
      <ST01>855</ST01>
      <ST02>0001</ST02>
      <BAK>
        <BAK01>11</BAK01>
        <BAK02>AT</BAK02>
        <BAK03>117624</BAK03>
        <BAK04>20120321</BAK04>
      </BAK>
      <DTM>
        <DTM01>097</DTM01>
        <DTM02>20120321</DTM02>
        <DTM03>162329</DTM03>
      </DTM>
      <SI>
        <SI01>TI</SI01>
        <SI02>IQ</SI02>
        <SI03>E</SI03>
      </SI>
      <N1>
        <N101>BY</N101>
        <N103>25</N103>
        <N104>180A</N104>
      </N1>
      <PO1>
        <PO101>1</PO101>
        <PO102>1</PO102>
        <PO103>EA</PO103>
        <PO106>A6</PO106>
        <PO107>CSA</PO107>
      </PO1>
      <N9>
        <N901>ME</N901>
        <N902>MLT</N902>
      </N9>
      <MTX>
        <MTX02>ATN: 10000001</MTX02>
      </MTX>
      <MTX>
        <MTX02>--LST</MTX02>
      </MTX>
      <MTX>
        <MTX02>SA 1040 BROADWAY , NEEDLES , CA 92363</MTX02>
      </MTX>
      <MTX>
        <MTX02>NP TSR Inc</MTX02>
      </MTX>
      <MTX>
        <MTX02>LA1 1040 E Broadway</MTX02>
      </MTX>
      <MTX>
        <MTX02>--BILL</MTX02>
      </MTX>
      <MTX>
        <MTX02>USI .............. 210053119900</MTX02>
      </MTX>
      <MTX>
        <MTX02>Address Line 1 ... ATTN DEPT 13</MTX02>
      </MTX>
      <MTX>
        <MTX02>Address Line 2 ... 2005 HUMMEL AVE</MTX02>
      </MTX>
      <MTX>
        <MTX02>--S&amp;E</MTX02>
      </MTX>
      <MTX>
        <MTX02>S&amp;E SEQ Description Rat</MTX02>
      </MTX>
      <MTX>
        <MTX02>B0ALL 1 I Block All Svc Providers .00</MTX02>
      </MTX>
      <MTX>
        <MTX02>B1LCP 1 Bus 1pty Loc Clg Pln-Meas 32.00</MTX02>
      </MTX>
      <MTX>
        <MTX02>CP699 1 Local Calling Plan .00</MTX02>
      </MTX>
      <MTX>
        <MTX02>--LPIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>Preferred Carrier ..........+0PIC NO PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>--PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>Preferred Carrier ..........+0PIC NO PIC</MTX02>
      </MTX>
      <MTX>
        <MTX02>--PROV</MTX02>
      </MTX>
      <MTX>
        <MTX02>Intercept Removal Date: 0/00/00</MTX02>
      </MTX>
      <MTX>
        <MTX02>Last SO to Update:063384164</MTX02>
      </MTX>
      <CTT>
        <CTT01>1</CTT01>
      </CTT>
    </ST>
  </GS>
</ISA>
So the output XML should look like:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <TXNUM>117624</TXNUM>
  <DTSENT>20120321162329</DTSENT>
  <PRESPC>000</PRESPC>
  <PRESPD>Completed</PRESPD>
  <unparsedCSR>
    <ATN>7603266875</ATN>
    <SECTION>
      <MTX>--LST</MTX>
      <MTX>SA 1040 BROADWAY , NEEDLES , CA 92363</MTX>
      <MTX>NP TSR Inc</MTX>
      <MTX>LA1 1040 E Broadway</MTX>
      <MTX>--BILL</MTX>
      <MTX>USI .............. 210053119900</MTX>
      <MTX>Address Line 1 ... ATTN DEPT 13</MTX>
      <MTX>Address Line 2 ... 2005 HUMMEL AVE</MTX>
      <MTX>--S&amp;E</MTX>
      <MTX>S&amp;E SEQ Description Rat</MTX>
      <MTX>B0ALL 1 I Block All Svc Providers .00</MTX>
      <MTX>B1LCP 1 Bus 1pty Loc Clg Pln-Meas 32.00</MTX>
      <MTX>CP699 1 Local Calling Plan .00</MTX>
      <MTX>--LPIC</MTX>
      <MTX>Preferred Carrier ..........+0PIC NO PIC</MTX>
      <MTX>--PIC</MTX>
      <MTX>Preferred Carrier ..........+0PIC NO PIC</MTX>
      <MTX>--PROV</MTX>
      <MTX>Intercept Removal Date: 0/00/00</MTX>
      <MTX>Last SO to Update:063384164</MTX>
    </SECTION>
XSLT code, here is my XSLT code which deal with the parts before in the input unparsedCSR in the output:
Code:
<xsl:stylesheet 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    exclude-result-prefixes="xs">
<xsl:template match="ISA">
<response>
  <TXNUM><xsl:value-of select="GS/ST/BAK[BAK01 = '11' and BAK02 = 'AT']/BAK03"/></TXNUM>
  <DTSENT><xsl:value-of select="GS/ST/DTM[DTM01 = '097']/DTM02"/><xsl:value-of select="GS/ST/DTM[DTM01 = '097']/DTM03"/></DTSENT>
  <xsl:choose>
    <xsl:when test="GS/ST/N9[N901 = '1Q']/N903 = 'PRESPC' ">
      <PRESPC><xsl:value-of select="GS/ST/N9[N901 = '1Q']/N902"/></PRESPC>
      <PRESPD><xsl:value-of select="GS/ST/MTX[1]/MTX02"/></PRESPD>
    </xsl:when>
    <xsl:when test="GS/ST/N9[N901 = '1Q']/N903 = 'RESPC' ">
      <RESPC><xsl:value-of select="GS/ST/N9[N901 = '1Q']/N902"/></RESPC>
      <RESPD><xsl:value-of select="GS/ST/MTX[1]/MTX02"/></RESPD>
    </xsl:when>
    <xsl:otherwise>
      <PRESPC>000</PRESPC>
      <PRESPD>Completed</PRESPD>
    </xsl:otherwise>
   </xsl:choose>
   <unparsedCSR>	
       <!--codes to deal the the ATN and grouping stuffs -->
   </unparsedCSR>
</response>
</xsl:template>

Last edited by JohnKiller; March 22nd, 2012 at 11:12 AM..
 
Old March 22nd, 2012, 12:43 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

To group the sections you would simply use xsl:for-each-group with the group-starting-with attribute.

e.g.:
Code:
<xsl:for-each-group select="//MTX[position()&gt;1]" group-starting-with="MTX[MTX02 = '--------------------------------------']">
    <SECTION>
        <xsl:apply-templates select="current-group()"/>
    </SECTION>
</xsl:for-each-group>
This would skip the first MTX record (the ATN one).

You would then have some other templates to actually output the stuff you wanted:

Code:
<xsl:template match="MTX[MTX02='--------------------------------------']"></xsl:template>

<xsl:template match="MTX">
   <MTX>
      <xsl:value-of select="*"/>
   </MTX>
</xsl:template>
So that is one template to output nothing for splitter elements, and another to output the MTX element.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
JohnKiller (March 22nd, 2012)
 
Old March 22nd, 2012, 12:59 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Thank you so much, Sam. What is the "select="//MTX[position()&gt;1]" in the for-each-group?
 
Old March 22nd, 2012, 01:00 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That selects all the MTX elements except the first one (the ATN one).
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 22nd, 2012, 01:02 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

OK, the position()&gt; seems wired to me, I never use such thing in my codes. what is the &gt for?
 
Old March 22nd, 2012, 01:06 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

it is position() > 1, but the > needs to be encoded as it is not valid in an XML attribute, so it becomes &gt; (greater than)
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 22nd, 2012, 01:07 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

I see, thank you so much for your time. I will try to apply these for my stylesheet. I really appreciate.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT grouping - need help Half-Saint XSLT 9 January 23rd, 2009 11:10 AM
Problem of grouping in XSLT LeoMathew XSLT 2 September 11th, 2008 09:38 AM
Grouping with XSLT 1.0 rodmcleay XSLT 1 January 14th, 2008 11:42 PM
XSLT 1.0 Grouping kwilliams XSLT 0 January 11th, 2006 06:30 PM
XSLT Grouping Help Missy XSLT 0 December 14th, 2005 10:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.