Microsoft KB Archive/318360

From BetaArchive Wiki
Knowledge Base


Article ID: 318360

Article Last Modified on 9/16/2003



APPLIES TO

  • Microsoft .NET Framework 1.0



This article was previously published under Q318360

SYMPTOMS

When the format pattern is set to "A.1", xsl:number generates the following exception error message:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

STATUS

This bug was corrected in Microsoft .NET Framework Class Libraries 1.1.

MORE INFORMATION

The xsl:number element inserts a formatted number in the result tree. The number to be inserted is specified by an expression that is contained in the value attribute. The expression is evaluated and the resulting object is converted to a number. The number is rounded to an integer, converted to a string, and then inserted in the result tree.

A format is a sequence of tokens that specifies the format that is to be used for each number in the list. If no format tokens exist, the default value of 1 is used. This value generates a sequence (for example, 1 2 ... 10 11 12 ...). Each number after the first is separated from the preceding number by the separator token that precedes the format token that is used to format that number. If no separator tokens exist, a period (.) is used.

Steps to Reproduce the Behavior

  1. In Microsoft Visual C# .NET, create a new Console Application project.
  2. Paste the following XML in a text file, and then save the file as C:\Test.xml:

    <?xml version='1.0'?>
    <bookstore xml:lang="en-usabcd" specialty="novel" xmlns:my="http://www.ns1">
        <book style="autobiography">
        <published>1953-09-22</published>
        <title>Book 1</title>
            <author id = "a1">
                <first-name>Joe</first-name>
                <last-name>Bob</last-name>
                <award>Trenton Literary Review Honorable Mention</award>
            </author>
        </book>
        <book style="textbook">
            <published>1999-01-01</published>
            <title>Book 2</title>
            <author id = "2">
                <first-name>Mary</first-name>
                <last-name>Bob</last-name>
                <publication>
                    Publication 1
                    <first-name>Mary</first-name>
                    <last-name>Bob</last-name>
                </publication>
            </author>
            <author id = "3">
                <first-name>Second</first-name>
                <last-name>Author</last-name>
            </author>
        </book>
        <magazine style="glossy" frequency="monthly">
            <title>Road and Track</title>
            <subscription price="24" per="year"/>
            <special_edition per_year="1">Yes</special_edition>
        </magazine>
        
    </bookstore>
    
                        
  3. Paste the following XSLT in a text file, and then save the file as C:\Test.xsl:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://my.com">
    
    <xsl:output encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
    
        <xsl:template match="/">
            <xsl:apply-templates select="//last-name" mode="formatting-tests"/>
        </xsl:template>
    
        <xsl:template match="last-name" mode="formatting-tests">
    
            <xsl:number level="multiple" count="last-name | publication | author | book" format="A.1"/><xsl:text>, </xsl:text>
    
            <xsl:value-of select="concat(../first-name, ' ', .)"/>
    
        </xsl:template>
    
    </xsl:stylesheet>
                        
  4. Use the following namespaces:

    using System;
    using System.Xml;
    using System.Xml.XPath;
    using System.Xml.Xsl;
    using System.IO;
                        
  5. Paste the following code in static void Main(string[] args):

    XmlDocument xmldoc = new XmlDocument ();
    //Load the XML document.
    xmldoc.Load("c:\\test.xml");
    
    XPathNavigator xpathdoc;
    xpathdoc = xmldoc.CreateNavigator();
    
    XslTransform xsltdoc = new XslTransform();
    xsltdoc.Load("c:\\test.xsl");
    //Create a FileStream to store the XSL output.
    FileStream fs = new FileStream("test2.xml", FileMode.Create);
    //Transform the XML document.
    xsltdoc.Transform(xpathdoc,null,fs);
                        
  6. Build and run the project. Note that the error occurs on the Transform method of the XslTransform object. You expect the following output:

    A.1.1, Joe BobB.1.1, Mary BobB.1.2.1, Mary BobB.2.1, Second Author
                        


REFERENCES

For more information about xsl:number, see the following Web site:

Keywords: kbbug kbpending KB318360