nested xsl:for-each seems not working

wxdqz

New Member
I hope this is a right alias to ask xsl questions, if not so, please helpto forward it to the right alias. Thanks!I've the following input XML document<X12_4010_850><LEBIZ><Src_Org_ID>Src_Org_ID_1</Src_Org_ID><Dest_Org_ID>Dest_Org_ID_1</Dest_Org_ID><Transaction_Set_ID>Transaction_Set_ID_1</Transaction_Set_ID></LEBIZ><BEGBEG02="BE" BEG03="BEG03_1" BEG05="1999-09-09"></BEG><CUR CUR02="CUR"></CUR><REF REF01="IA" REF02="367599"/><REF REF01="DP" REF02="REF02_2" REF03="REF03_2"></REF><REF REF01="AB" REF02="REF02_2" REF03="REF03_2"></REF><SACLoop1><SAC SAC01="S" SAC02="SAC0" SAC03="SA" SAC04="SAC04_1" SAC05="1.0"SAC06="S" SAC07="1.0" SAC08="1.0" SAC13="SAC13_1" SAC15="SAC15_1"></SAC></SACLoop1><SACLoop1><SACSAC01="S" SAC02="SAC0" SAC03="SA" SAC04="SAC04_2" SAC05="2.0" SAC06="S" SAC07="2.0"SAC08="2.0" SAC13="SAC13_2" SAC15="SAC15_2"></SAC></SACLoop1><PO1Loop1><PO1PO101="PO101_1" PO102="1.0" PO103="PO" PO110="PO" PO111="PO111_1" PO113="PO113_1"PO115="PO115_1"></PO1><SACLoop2><SAC_2 SAC01="S" SAC02="SAC0" SAC05="1.0"SAC06="S" SAC07="1.0" SAC08="1.0"></SAC_2></SACLoop2><SACLoop2><SAC_2 SAC01="S"SAC02="SAC0" SAC05="2.0" SAC06="S" SAC07="2.0" SAC08="2.0"></SAC_2></SACLoop2></PO1Loop1><PO1Loop1><PO1PO101="PO101_2" PO102="2.0" PO103="PO" PO110="PO" PO111="PO111_2" PO113="PO113_2"PO115="PO115_2"></PO1><SACLoop2><SAC_2 SAC01="S" SAC02="SAC0" SAC05="3.0"SAC06="S" SAC07="3.0" SAC08="3.0"></SAC_2></SACLoop2><SACLoop2><SAC_2 SAC01="S"SAC02="SAC0" SAC05="4.0" SAC06="S" SAC07="4.0" SAC08="4.0"></SAC_2></SACLoop2></PO1Loop1></X12_4010_850>which contains the following structure,...<CUR CUR02="CUR"></CUR><REF REF01="IA" REF02="367599"/><REF REF01="DP" REF02="REF02_2" REF03="REF03_2"></REF><REF REF01="AB" REF02="REF02_2" REF03="REF03_2"></REF><SACLoop1>...when I use the following XSL to do the transformation:-<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'xmlns:var='urn:var' xmlns:user='urn:user' exclude-result-prefixes='msxslvar user' version='1.0'><xsl:output method='xml' omit-xml-declaration='yes' /><xsl:template match='/'><xsl:apply-templates select='X12_4010_850'/></xsl:template><xsl:template match='X12_4010_850'><LeBizPO><po><xsl:for-each select='REF'><xsl:if test='position()=3'><xsl:attribute name='curr_pos'><xsl:value-of select='3'/></xsl:attribute></xsl:if><xsl:variable name='var:v1' select='user:fctequal(string(@REF01),"DP")'/><xsl:if test='string($var:v1)="true"'><xsl:variable name='var:v2' select='@REF02'/><!-- <xsl:attribute name='category_number'><xsl:value-of select='$var:v2'/></xsl:attribute>--><xsl:attribute name='category_number'><xsl:value-of select='position()'/></xsl:attribute></xsl:if><xsl:variable name='var:v3' select='user:fctequal(string(@REF01),"DP")'/><xsl:if test='string($var:v3)="true"'><xsl:variable name='var:v4' select='@REF03'/><xsl:attribute name='division_name'><xsl:value-of select='$var:v4'/></xsl:attribute></xsl:if><xsl:variable name='var:v5' select='user:fctequal(string(@REF01),"PD")'/><xsl:if test='string($var:v5)="true"'><xsl:variable name='var:v6' select='@REF02'/><xsl:attribute name='event_number'><xsl:value-of select='$var:v6'/></xsl:attribute></xsl:if><xsl:variable name='var:v7' select='user:fctequal(string(@REF01),"PD")'/><xsl:if test='string($var:v7)="true"'><xsl:variable name='var:v8' select='@REF03'/><xsl:attribute name='event_name'><xsl:value-of select='$var:v8'/></xsl:attribute></xsl:if><!-- Connection from source node "BEG05" to destination node "order_date"--><xsl:attribute name='order_date'><xsl:value-of select='ancestor::*[1]/BEG/@BEG05'/></xsl:attribute><xsl:variable name='var:v9' select='user:fctstringsubstr(string(ancestor::*[1]/N1Loop1/N1/@N104),"4","7")'/><xsl:attribute name='ship_to_location'><xsl:value-of select='$var:v9'/></xsl:attribute><!-- Connection from source node "BEG02" to destination node "order_type"--><xsl:attribute name='order_type'><xsl:value-of select='ancestor::*[1]/BEG/@BEG02'/></xsl:attribute><xsl:for-each select='ancestor::*[1]/PO1Loop1'><po_detail></po_detail></xsl:for-each><xsl:for-each select='ancestor::*[1]/N9Loop1'><msg><xsl:variable name='var:v23' select='user:fctequal(string(N9/@N901),"K6")'/><xsl:if test='string($var:v23)="true"'><xsl:variable name='var:v24' select='N9/@N902'/><xsl:attribute name='msg_text'><xsl:value-of select='$var:v24'/></xsl:attribute></xsl:if></msg></xsl:for-each></xsl:for-each></po></LeBizPO></xsl:template><msxsl:script language='VBScript' implements-prefix='user'><![CDATA[Function FctEqual( p_strA, p_strB )If LCase( p_strA ) = "true" Thenp_strA = trueElseIf LCase( p_strA ) = "false" Thenp_strA = falseEnd IfIf LCase( p_strB ) = "true" Thenp_strB = trueElseIf LCase( p_strB ) = "false" Thenp_strB = falseEnd IfIf ( IsNumeric( p_strA ) And IsNumeric( p_strB ) ) Thenp_strA = CDbl( p_strA )p_strB = CDbl( p_strB )End IfIf ( p_strA = p_strB ) ThenFctEqual = trueElseFctEqual = falseEnd IfEnd FunctionFunction FctStringSubstr( p_strA, p_cchLeft, p_cchRight )If ( IsNumeric( p_cchLeft ) And IsNumeric( p_cchRight ) ) Thenp_cchLeft = CLng(p_cchLeft)p_cchRight = CLng(p_cchRight)If ( p_cchLeft > 0 And ( p_cchRight >= p_cchLeft ) ) ThenFctStringSubstr = mid( p_strA, p_cchLeft, p_cchRight - p_cchLeft+ 1 )ElseFctStringSubstr = ""End IfElseFctStringSubstr = ""End IfEnd Function]]></msxsl:script></xsl:stylesheet>I always can't produce 'category_number', 'division_name' attribute in theoutput XML doc. I debugged it and found that ONLY 1st iteration of REF isselected when I added the following debugging code underneath <xsl:for-eachselect='REF'> element.<xsl:if test='position()=1'><xsl:attribute name='curr_pos'><xsl:value-of select='1'/></xsl:attribute></xsl:if>However, if I removed<xsl:for-each select='ancestor::*[1]/PO1Loop1'>...<xsl:for-each> block, 2nd and 3rd iteration of REF is selected by the REFfor-each block. And category_number' and 'division_name' attribute can begenerated at <po> element, like the following:-<po po_id="Transaction_Set_ID_1" sender_id="Src_Org_ID_1" receiver_id="Dest_Org_ID_1"po_number="BEG03_1" currency_code="CUR" category_number="1" division_name="REF03_2"order_date="1999-09-09" ship_to_location="" order_type="BE">What did I do wrong? How to fix it? Thanks!
 
Back
Top