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:
- 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.
- 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
- 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>
- Name the document Employees.xml.
- 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>
- Name the style sheet Employees.xsl.
- Start Visual Studio .NET 2003.
- On the File menu, point to New, and then click Project.
- Click Visual C# Projects under Project Types, and then click Console Application under Templates.
- Copy or move the XML and XSLT documents that you created in steps 1
and 2 to the bin\debug folder of the project.
- 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();
}
- Save the project.
- 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.
- 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.
- Rerun the project. Notice that the transformation completes
successfully.
APPLIES TO
- Microsoft .NET Framework 1.1
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