How to reference values from outer loop??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redgrL86
    New Member
    • Jun 2007
    • 7

    How to reference values from outer loop??

    Hi,
    I am working on an XSL stylesheet and I have a loop within a loop where I need to compare element values from the inside loop to values from the outer loop (see bolded line in XSL below). The inside loop references elements from a second XML document, which makes this especially difficult (at least difficult for me).
    The outermost (first) loop references elements in the Primary XML file (the one to which the XSL is applied directly) and the inner loop references elements in the Second XML file.

    Hopefully this makes sense...

    Thank you so much!!
    Christine


    XSL:
    <?xml version="1.0"?>
    <xsl:styleshe et version="2.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <xsl:variable name="file2" select="documen t('C:\Documents and Settings\e\My Documents\Trial XML\Recipe Code\UP_X783X_U FDF_CLEANING.XM L')"/>
    <html>
    <body>
    <table border="0" width="100%">
    <tr align="left">
    <th>Step</th>
    <th>Parameter </th>
    <th>Origin</th>
    <th>Low</th>
    <th>Value</th>
    </tr>
    <xsl:for-each select="RecipeE lement/Steps/Step">
    <xsl:sort select="@YPos" data-type="number"/>
    <xsl:sort select="@XPos" data-type="number"/>
    <xsl:for-each select="Formula Value">
    <tr>
    <td>
    <xsl:value-of select="ancesto r::Step/Name/text()"/>
    </td>
    <td>
    <xsl:value-of select="Name"/>
    </td>
    <xsl:if test="Value">
    <td>Value</td>
    <xsl:if test="Real">
    <xsl:for-each select="$file2/RecipeElement/Parameter">
    <xsl:if test="matches(' Name/text()','Formul aValue/Name/text()=current( )')"> <td>
    0
    </td>
    </xsl:if>
    </xsl:for-each>
    <td>
    <xsl:value-of select="Real"/>
    </td>
    <td>
    High
    </td>
    </xsl:if>
    </xsl:if>
    </tr>
    </xsl:for-each>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>


    Primary XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <RecipeElemen t>
    <Step XPos="600" YPos="598" AcquireUnit="tr ue">
    <Name>UP_X783X_ UFDF_CLEANING:1 </Name>
    <StepRecipeID>U P_X783X_UFDF_CL EANING</StepRecipeID>
    <UnitAlias>UP_X 783X_UFDF_CLEAN ING:1</UnitAlias>
    <FormulaValue >
    <Name>CIP_FEED_ PUMP_SPEED</Name>
    <Display>fals e</Display>
    <Value/>
    <Real>75</Real>
    <EngineeringUni ts/>
    </FormulaValue>
    <FormulaValue >
    <Name>CIP_XFER_ PUMP_SPEED</Name>
    <Display>fals e</Display>
    <Value/>
    <Real>35</Real>
    <EngineeringUni ts/>
    </FormulaValue>
    </Step>
    </RecipeElement>


    Second XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <RecipeElemen t>
    <Parameter>
    <Name>CIP_FEED_ PUMP_SPEED</Name>
    <ERPAlias/>
    <PLCReference>1 </PLCReference>
    <Real>45</Real>
    <High>100</High>
    <Low>0</Low>
    <EngineeringUni ts>%</EngineeringUnit s>
    <Scale>false</Scale>
    </Parameter>
    <Parameter>
    <Name>CIP_XFER_ PUMP_SPEED</Name>
    <ERPAlias/>
    <PLCReference>1 </PLCReference>
    <Real>30</Real>
    <High>100</High>
    <Low>0</Low>
    <EngineeringUni ts>%</EngineeringUnit s>
    <Scale>false</Scale>
    </Parameter>
    </RecipeElement>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Strangely enough, I had this problem 2 weeks ago. Before the for-each, create a variable pointing to your current location. Then reference that variable. In your case, you could simply make a variable pointing to the value of the Name element.

    Code:
    <xsl:if test="Real">
    [B]<xsl:variable name="Name" select="Name"/>[/B]
    <xsl:for-each select="$file2/RecipeElement/Parameter">
    <xsl:if test="matches('Name/text()'[B],$Name)"> [/B] <td>
    0
    </td>
    </xsl:if>
    Further simplifying, you could do this check in the for-each loop
    Code:
    <xsl:if test="Real">
    <xsl:for-each select="$file2/RecipeElement/Parameter[B][Name = current()/$Name]"[/B]>
    <td>
    0
    </td>
    </xsl:if>

    Comment

    Working...