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



The information in this article 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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Use the following XML to create an XML document:
    <?xml version="1.0" encoding="utf-8" ?> 
    <EMPLOYEES>
          <EMPLOYEE>
                <NAME>Steve</NAME>
                <DEPT>IT</DEPT>
                <SKILL>C++</SKILL>
                <SKILL>C#</SKILL>             
          </EMPLOYEE>       
          <EMPLOYEE>              
                <NAME>John</NAME>
                <DEPT>IT</DEPT>
                <SKILL>VB.NET</SKILL>
                <SKILL>SQl Server</SKILL>
          </EMPLOYEE>             
    </EMPLOYEES>
    
  2. Name the document Employees.xml.
  3. Use the following XSLT code to create a style sheet:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"   
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      
          
          <xsl:preserve-space elements="*" />
          
          <xsl:template match="/EMPLOYEES">
            <!--xsl:if test="EMPLOYEE[DEPT='Finance']"-->
    			<xsl:for-each select="EMPLOYEE[DEPT='Finance']">                                   
    			   <xsl:sort select="NAME"/>        
    			   <xsl:value-of select="NAME"/>         
    			</xsl:for-each>		
    		<!--/xsl:if-->
    	  </xsl:template>	  
    	           
    </xsl:stylesheet>
    
  4. Name the style sheet Employees.xsl.
  5. Start Visual Studio .NET 2003.
  6. On the File menu, point to New, and then click Project.
  7. Click Visual C# Projects under Project Types, and then click Console Application under Templates.
  8. Copy or move the XML and XSLT documents that you created in steps 1 and 2 to the bin\debug folder of the project.
  9. 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("Employee.xsl"); 
    				
    stylesheet.Transform("Employee.xml","output.html"); 
    
    Console.WriteLine("Done");	
       }
       catch(Exception e)
       {
    		
    Console.WriteLine(e.ToString()); 
       }
       finally
       {
    	Console.ReadLine(); 
    }
    
  10. Save the project.
  11. Build and run the project. Notice that a "System.ArgumentOutOfRange" 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.
  12. Stop the project, and then implement one of the following changes to the Employees.xsl style sheet:
    • Comment out the xsl:preserve-space element.
    • 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.
  13. Rerun the project. Notice that the transformation completes successfully.

Modification Type:MajorLast Reviewed:10/1/2003
Keywords:kbbug KB829014 kbAudDeveloper