PRB: The XSLT current() method may return incorrect data when try to count the number of consecutive elements with the same key value (841763)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft XML 3.0 SP3
  • Microsoft XML 4.0 SP1

SYMPTOMS

You use the Extensible Stylesheet Language for Transformations (XSLT) current() method when transforming XML data. When try to count the number of consecutive elements with the same key value, you may receive incorrect results.

RESOLUTION

Use the workaround that is suggested in this article, or upgrade to MSXML 4.0 Service Pack 2 (SP2).

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section. This problem was corrected in MSXML 4.0 Service Pack 2.

MORE INFORMATION

Steps to reproduce the behavior

  1. Paste the following XML code in Notepad, and then save the file as prod.xml.
    <?xml version="1.0"?>
    <Products>
     <Product name = "x"/>
     <Product name = "x"/>
     <Product name = "y"/>
     <Product name = "z"/>
     <Product name = "z"/>
     <Product name = "x"/>
    </Products>
    
  2. Paste the following XML code in Notepad to create the XSL style sheet, and then save the file as prod.xsl.
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output omit-xml-declaration="yes" indent="yes"/>
      <xsl:strip-space elements="*" />
      <xsl:key name="kSibl" match="Product"
    use="generate-id(following-sibling::Product[@name != current()/@name][1])"/>
      <xsl:template match="Products">
        <Products>
          <xsl:apply-templates select="Product[1]"/>
        </Products>
      </xsl:template>
      <xsl:template match="Product">
        <Product name="{@name}"
    count="{count(key('kSibl',generate-id(following-sibling::Product[@name !=
    current()/@name][1])))}"/>
        <xsl:apply-templates select="following-sibling::Product[@name !=
    current()/@name][1]"/>
      </xsl:template>
    </xsl:stylesheet>
    
  3. Create new Microsoft Visual C# .NET Console Application project in Microsoft Visual Studio .NET 2003.
  4. Paste the following code in your project.

    Note Change the paths to the .xml file and the .xsl file depending on your environment.
    
    using System;
    using System.Xml;
    using System.Xml.Xsl;
    using System.Diagnostics;
    
    namespace XSLPrb {
    	class Class1 		{
    		[STAThread]
    		static void Main(string[] args)		{
    			try {
    				XslTransform myTransform = new XslTransform();
    				myTransform.Load(@"\<Path_To_XSL_File>\prod.xsl");
    				myTransform.Transform(@"\<Path_To_XML_File>\prod.xml", @"\<Path_To_Output_XML_File>\prodOut.xml");
    			}
    			catch (Exception e) {
    				Debug.WriteLine(e.Message + "\n" + e.StackTrace);
    			}
    		}
    	}
    }
    
  5. Save and run your project.
You expect the following output.
<Products>
  <Product name="x" count="2" />
  <Product name="y" count="1" />
  <Product name="z" count="2" />
  <Product name="x" count="1" />
</Products>
However, you receive the following output.
<Products>
  <Product name="x" count="2" />
  <Product name="y" count="1" />
  <Product name="z" count="0" />
  <Product name="x" count="2" />
</Products>

WORKAROUND

Replace the code in step 2 of the "Steps to reproduce the behavior" section. To do this, paste the following code in Notepad to create the XSL style sheet, and then save the file as prod.xsl.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:hashtable="urn:hashtable">
      <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <msxsl:script language="C#" implements-prefix="hashtable">
            System.Collections.Hashtable table = new System.Collections.Hashtable();
            public string Add(string key) {
                  if (table.ContainsKey(key)) {
                        table[key] = (int)table[key] + 1;
                  }
                  else {
                        table.Add(key, 1);
                  }
                  return string.Empty;
            }
            public int Get(string key) {
                  if (table.ContainsKey(key)) {
                        return (int)table[key];
                  }
                  else {
                        return 0;
                  }
            }
    </msxsl:script>
      <xsl:strip-space elements="*" />
      <xsl:variable name="dummy">
            <xsl:for-each select="//Product">
                  <xsl:value-of select="hashtable:Add(generate-id(following-sibling::Product[@name != current()/@name][1]))"/>
            </xsl:for-each>
      </xsl:variable>
      <xsl:template match="Products">
            <Products>
                  <xsl:apply-templates select="Product[1]"/> 
            </Products>
      </xsl:template>
      <xsl:template match="Product">
            <Product name="{@name}" count="{hashtable:Get(generate-id(following-sibling::Product[@name != current()/@name][1]))}"/>
            <xsl:apply-templates select="following-sibling::Product[@name !=current()/@name][1]"/>
      </xsl:template>
</xsl:stylesheet>
When you modify the XSL style sheet, make sure that you do the following:
  • Locate the following code.
    <xsl:key name="key-name" match="pattern" use="string-expression" />
    Replace it with the following code.
    <xsl:for-each select="//pattern">
          <xsl:value-of select="key:Add('key-name', string-expression, .)"/>
    </xsl:for-each>
  • Locate the following code.
    key(key-name, string-expression)
    Replace it with the following code.
    key:Get(key-name, string-expression)

Modification Type:MajorLast Reviewed:1/11/2005
Keywords:kbprb KB841763 kbAudDeveloper