Microsoft KB Archive/829014

= BUG: XslTransform: Sorting an Empty Node Set Raises an Exception in the .NET Framework 1.1 =

Article ID: 829014

Article Last Modified on 10/1/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1

-





SYMPTOMS
When you try to sort an empty node set in an XSL Transformation (XSLT) that you execute by using an instance of the System.Xml.Xsl.XslTransform type in the .NET Framework 1.1, you may receive the following error message:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index



CAUSE
This problem occurs only when a white space rule has been defined in the style sheet by using the xsl:preserve-space or the xsl:strip-space XSLT elements.



RESOLUTION
To work around this problem, use one of the following methods:
 * 1) Method 1: Remove the white space rule (xsl:preserve-space or xsl:strip-space) definitions from the style sheet. This may not always be an option because the defined white space rules may have related functional requirements.
 * 2) Method 2: Conditionally execute the sort after you test for the existence of nodes in the node set.



STATUS
Microsoft has confirmed that this is a bug in the .NET Framework 1.1 implementation of the System.Xml.Xsl.XslTransform type.



Steps to Reproduce the Behavior
  Use the following XML to create an XML document:    Steve IT C++</SKILL> <SKILL>C#</SKILL> </EMPLOYEE> <EMPLOYEE> John</NAME> IT</DEPT> <SKILL>VB.NET</SKILL> <SKILL>SQl Server</SKILL> </EMPLOYEE> </EMPLOYEES> </li> <li>Name the document Employees.xml .</li> <li> Use the following XSLT code to create a style sheet: <?xml version=&quot;1.0&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:preserve-space elements=&quot;*&quot; /> <xsl:template match=&quot;/EMPLOYEES&quot;> <xsl:for-each select=&quot;EMPLOYEE[DEPT='Finance']&quot;> <xsl:sort select=&quot;NAME&quot;/> <xsl:value-of select=&quot;NAME&quot;/> </xsl:for-each> </xsl:template> </xsl:stylesheet> </li> <li>Name the style sheet Employees.xsl .</li> <li>Start Visual Studio .NET 2003.</li> <li>On the File menu, point to New, and then click Project.</li> <li>Click Visual C# Projects under Project Types, and then click Console Application under Templates.</li> <li>Copy or move the XML and XSLT documents that you created in steps 1 and 2 to the bin\debug folder of the project.</li> <li> To execute an XSLT transformation by using the Employees.xml and Employees.xsl XML and XSLT files, paste the following C# code in the start up Main method: try {   XslTransform stylesheet = new XslTransform; stylesheet.Load(&quot;Employee.xsl&quot;); stylesheet.Transform(&quot;Employee.xml&quot;,&quot;output.html&quot;);

Console.WriteLine(&quot;Done&quot;); }  catch(Exception e)   { Console.WriteLine(e.ToString); }  finally {   Console.ReadLine; } </li> <li>Save the project.</li> <li>Build and run the project. Notice that a &quot;System.ArgumentOutOfRange&quot; exception is raised and reported in the Console window. The exception occurs because the XPath expression that is used to retrieve the node set that must be sorted does not have any matching nodes in the data. Therefore, an empty node set is returned. When you try to apply a sort, you receive the exception.</li> <li>Stop the project, and then implement one of the following changes to the Employees.xsl style sheet: <ul> <li>Comment out the xsl:preserve-space element.</li> <li>Uncomment the <xsl:if...> element and the </xsl:if> element to activate the conditional test for the node set that must be processed by the xsl:for-each loop, and then sorted.</li></ul> </li> <li>Rerun the project. Notice that the transformation completes successfully.</li></ol>

Keywords: kbbug KB829014

-

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

© Microsoft Corporation. All rights reserved.