Link to home
Start Free TrialLog in
Avatar of NAEDI2
NAEDI2Flag for United States of America

asked on

Xpath Expression Help

In the attached stylesheet I need to transform the XML to add various counters the trailer elements.  An example is the CTT_347 element that needs to contain the count of the IT1_01 nodes within the same SG0.  My code worked well until I had two SG0 nodes in the file.  The second CTT_347 node gets the total of all the IT1_01 nodes from all the previous SG0 nodes as well as the current one which is wrong.

Is it possible with a single Xpath expression to get a count of only the IT1_01 nodes within the current SG0 node?

   <xsl:template name="HL_segments">
      <xsl:choose>
         <xsl:when test="name() = 'CTT' ">
            <xsl:element name="CTT">
               <xsl:element name="idCTT">CTT</xsl:element>
               <xsl:element name="CTT_354">
                  <xsl:value-of select="count(preceding::IT1_01)"/>
               </xsl:element>
            </xsl:element>
         </xsl:when>


<?xml version="1.0" encoding="ISO-8859-1"?>
<ACTIS>
   <ISA>
   </ISA>
   <GS>
   </GS>
   <SG0>
      <ST>
         <idST/>
         <ST_143>810</ST_143>
         <ST_329>0001</ST_329>
      </ST>
      <BIG>
      </BIG>
      <IT1_01>
      </IT1_01>
      <IT1_01>
      </IT1_01>
      <IT1_01>
      </IT1_01>
      <IT1_01>
      </IT1_01>
      <CTT>
         <idCTT/>
         <CTT_354/>
         <CTT_347/>
         <CTT_81/>
         <CTT_355_01/>
         <CTT_183/>
         <CTT_355_02/>
         <CTT_352/>
      </CTT>
      <SE>
      </SE>
   </SG0>
   <SG0>
      <ST>
         <idST/>
         <ST_143>810</ST_143>
         <ST_329>0002</ST_329>      
      </ST>
      <BIG>
      </BIG>
      <IT1_01>
      </IT1_01>
      <IT1_01>
      </IT1_01>
      <IT1_01>
      </IT1_01>
      <CTT>
         <idCTT/>
         <CTT_354/>
         <CTT_347/>
         <CTT_81/>
         <CTT_355_01/>
         <CTT_183/>
         <CTT_355_02/>
         <CTT_352/>
      </CTT>
      <SE>
      </SE>
   </SG0>  
   <GE>
   </GE>
   <IEA>
   </IEA>
</ACTIS>
desadv_x12_856_4010.desadv_x12_856_4.xsl
Edit2.xml
ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NAEDI2

ASKER

You would think so but nope.  I am very new to XSL but that should have been obvious.   Thank you!
You are welcome!