Microsoft KB Archive/324033

= BUG: Position and Last XPath Functions Are Ignored When You Use Them as XSLT Function Arguments =

Article ID: 324033

Article Last Modified on 7/16/2004

-

APPLIES TO


 * Microsoft .NET Framework 1.0

-



This article was previously published under Q324033



SYMPTOMS
The position and last XPath functions are ignored when you use them directly as XSLT function (standard, script, and extension function) arguments in XPath query expressions that are used in XSLT style sheets.



CAUSE
The XPath processor executes the XSLT functions before it sets the context of the position and last XPath functions.



RESOLUTION
Use XSLT variables to store the values that these functions return. You can then use the XSLT variables as the XSLT function arguments in XPath query expressions that are used in the style sheet.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
Note that this problem does not occur when the position and last functions are used in a predicate in an expression that is supplied as an XSLT function argument.

Steps to Reproduce the Behavior
 In Microsoft Visual Studio .NET, create a new Visual Basic .NET Console Application project.  Use the following code to create and add an XML document named Repro.xml to the project:   Use the following code to create and add an XSLT style sheet named Repro.xsl to the project. Study the inline comments to understand the functionality of the code in the style sheet: 



 

</xsl:stylesheet> </li>  Paste the following code in the Sub Main procedure in Module1.vb to execute the XSLT transformation by applying Repro.xsl to Repro.xml: Dim transform As New System.Xml.Xsl.XslTransform transform.Load(&quot;..\Repro.xsl&quot;) transform.Transform(&quot;..\Repro.xml&quot;, &quot;..\output.xml&quot;) Console.WriteLine(&quot;Done&quot;) Dim response As String = Console.ReadLine </li> Save and run the project. The code in the Sub Main procedure executes the XSLT transformation and writes the transformation output to a file named Output.xml in the project folder. The message &quot;Done&quot; appears in the console window when the transformation completes. Press any key to close the console window and return to the development environment.</li>  In Solution Explorer, click Refresh button to view the Output.xml file in the project folder. When you open the Output.xml file, you will see that it contains the following XML: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> The value elements in the source XML whose values match their position are not listed in the output as expected. The <Position3Element> element, however, has the correct value because the expression that is used in the style sheet to retrieve it uses the position function in a predicate (instead of directly as an XSLT function argument).

</li> Close Output.xml, and then open the Repro.xsl style sheet.</li>  Replace the <xsl:for-each> block that is used to generate the contents of the <ValuesThatMatchPosition> element with the following code to store the output of the position XPath function in an XSLT variable and supply the variable as the value parameter in the call to the key XSLT function: <xsl:for-each select=&quot;value&quot;> <xsl:variable name=&quot;pos&quot; select=&quot;position&quot;/> <xsl:if test=&quot;key('Values',$pos)&quot;> <xsl:value-of select=&quot;.&quot;/> </xsl:if> </xsl:for-each> </li>  Save the changes to the style sheet, and then reexecute the project. When you view the contents of Output.xml, you see the following XML: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> The output now lists the value elements in the source XML whose values match their position. </li></ol>

Keywords: kbbug kbpending KB324033

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.