Microsoft Knowledge Base Email Alertz

KBAlertz.com: 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:...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 829014 - Last Review: October 1, 2003 - Revision: 2.1

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

On This Page

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="http://support.microsoft.com/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.

APPLIES TO
  • Microsoft .NET Framework 1.1
Keywords: 
kbbug KB829014
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please