Microsoft KB Archive/841763

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

Article ID: 841763

Article Last Modified on 1/11/2005

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft XML Parser 3.0 Service Pack 3
 * Microsoft XML Core Services 4.0 Service Pack 1

-



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 &quot;Applies to&quot; section. This problem was corrected in MSXML 4.0 Service Pack 2.



Steps to reproduce the behavior
  Paste the following XML code in Notepad, and then save the file as prod.xml.            Paste the following XML code in Notepad to create the XSL style sheet, and then save the file as prod.xsl. <?xml version=&quot;1.0&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:output omit-xml-declaration=&quot;yes&quot; indent=&quot;yes&quot;/> <xsl:strip-space elements=&quot;*&quot; /> <xsl:key name=&quot;kSibl&quot; match=&quot;Product&quot; use=&quot;generate-id(following-sibling::Product[@name != current/@name][1])&quot;/> <xsl:template match=&quot;Products&quot;>  <xsl:apply-templates select=&quot;Product[1]&quot;/> </Products> </xsl:template> <xsl:template match=&quot;Product&quot;> <Product name=&quot;{@name}&quot; count=&quot;{count(key('kSibl',generate-id(following-sibling::Product[@name != current/@name][1])))}&quot;/> <xsl:apply-templates select=&quot;following-sibling::Product[@name != current/@name][1]&quot;/> </xsl:template> </xsl:stylesheet> </li> Create new Microsoft Visual C# .NET Console Application project in Microsoft Visual Studio .NET 2003.</li>  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(@&quot;\<Path_To_XSL_File>\prod.xsl&quot;); myTransform.Transform(@&quot;\<Path_To_XML_File>\prod.xml&quot;, @&quot;\<Path_To_Output_XML_File>\prodOut.xml&quot;); }           catch (Exception e) { Debug.WriteLine(e.Message + &quot;\n&quot; + e.StackTrace); }       }    } } </li> Save and run your project.</li></ol>

You expect the following output.  <Product name=&quot;x&quot; count=&quot;2&quot; /> <Product name=&quot;y&quot; count=&quot;1&quot; /> <Product name=&quot;z&quot; count=&quot;2&quot; /> <Product name=&quot;x&quot; count=&quot;1&quot; /> </Products> However, you receive the following output.  <Product name=&quot;x&quot; count=&quot;2&quot; /> <Product name=&quot;y&quot; count=&quot;1&quot; /> <Product name=&quot;z&quot; count=&quot;0&quot; /> <Product name=&quot;x&quot; count=&quot;2&quot; /> </Products>

<div class="workaround_section">

WORKAROUND
Replace the code in step 2 of the &quot;Steps to reproduce the behavior&quot; 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=&quot;1.0&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:msxsl=&quot;urn:schemas-microsoft-com:xslt&quot; xmlns:hashtable=&quot;urn:hashtable&quot;> <xsl:output omit-xml-declaration=&quot;yes&quot; indent=&quot;yes&quot;/> <msxsl:script language=&quot;C#&quot; implements-prefix=&quot;hashtable&quot;> 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=&quot;*&quot; /> <xsl:variable name=&quot;dummy&quot;> <xsl:for-each select=&quot;//Product&quot;> <xsl:value-of select=&quot;hashtable:Add(generate-id(following-sibling::Product[@name != current/@name][1]))&quot;/> </xsl:for-each> </xsl:variable> <xsl:template match=&quot;Products&quot;>  <xsl:apply-templates select=&quot;Product[1]&quot;/> </Products> </xsl:template> <xsl:template match=&quot;Product&quot;> <Product name=&quot;{@name}&quot; count=&quot;{hashtable:Get(generate-id(following-sibling::Product[@name != current/@name][1]))}&quot;/> <xsl:apply-templates select=&quot;following-sibling::Product[@name !=current/@name][1]&quot;/> </xsl:template> </xsl:stylesheet> When you modify the XSL style sheet, make sure that you do the following: <ul>  Locate the following code. <xsl:key name=&quot;key-name&quot; match=&quot;pattern&quot; use=&quot;string-expression&quot; /> Replace it with the following code. <xsl:for-each select=&quot;//pattern&quot;> <xsl:value-of select=&quot;key:Add('key-name', string-expression, .)&quot;/> </xsl:for-each> </li>  Locate the following code. key(key-name, string-expression) Replace it with the following code. key:Get(key-name, string-expression) </li></ul>

Additional query words: System.xml, XSL, scripts

Keywords: kbprb KB841763

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.