'Maintaining Context in XSLT Functions
For the following XML:
<X12>
<GROUP>
<TS_850>
<REF>
<REF01>CR</REF01>
<REF02>53222</REF02>
</REF>
</TS_850>
</GROUP>
</X12>
This code works:
<xsl:variable name="REF02">
<xsl:for-each select="X12/GROUP/TS_850/REF">
<xsl:if test="REF01='CR'">
<xsl:value-of select="REF02"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
...
<xsl:value-of select="$REF02"/>
However, this code does not:
<xsl:function name="test:valueWhereValue">
<xsl:param name="context" />
<xsl:param name="conditionalElement" />
<xsl:param name="conditionalValue" />
<xsl:param name="outputElement"/>
<xsl:for-each select="$context">
<xsl:if test="$conditionalElement = $conditionalValue">
<xsl:value-of select="$outputElement" />
</xsl:if>
</xsl:for-each>
</xsl:function>
...
<xsl:copy-of select="test:valueWhereValue(X12/GROUP/TS_850/REF, REF01, 'CR', REF02)"></xsl:copy-of>
As best I can tell, the two code snippets should be functionally equivalent. However, according to my debugger, REF01 and REF02 don't actually reference nodes the way I would expect in the function version. It seems like I'm not actually entering the proper context with my for-each statement in the function, which prevents the children nodes from selecting properly. Why is that, and how can I fix my function?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
