Microsoft Knowledge Base Email Alertz

KBAlertz.com: (841763) - Explains the problem that customers may encounter when they try to use the XSLT current() method while counting the number of consecutive elements with the same key value. Describes a workaround to fix this problem.

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: 841763 - Last Review: January 11, 2005 - Revision: 1.1

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

On This Page

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

APPLIES TO
  • Microsoft .NET Framework 1.1
  • Microsoft XML Parser 3.0 Service Pack 3
  • Microsoft XML Core Services 4.0 Service Pack 1
Keywords: 
kbprb KB841763
       

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