Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XSL:Count on a passed parameter

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 7/31/2008 2:05:00 PM

Kevin Burton wrote:
> You are right the agument is passed as an XmlDocument. Now knowing this, how 
> do get the number of nodes in the transform like I would if it was a "real" 
> document. If I knew that it was an XmlDocument I could do something like 
> $IdenticalList.SelectNodes("/root/list/sku").Count but that is obviously not 
> going to work inside a style sheet.

I don't know what the problem is and I can't reproduce it.

The stylesheet I have used is as follows:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:param name="foo"/>

   <xsl:template match="/">
     <result>
       <xsl:value-of select="count($foo/root/item)"/>
     </result>
   </xsl:template>

</xsl:stylesheet>

Then I have written the following .NET 2.0 code to test that:

     class Program
     {
         static void Main(string[] args)
         {
             TestCount(new string[] { "1", "2", "3", "4", "5" });
             TestCount(new string[] { "1" });
             TestCount(new string[] { });
         }

         static void TestCount(IEnumerable<string> items)
         {
             XslCompiledTransform xsltProc = new XslCompiledTransform();
             xsltProc.Load(@"..\..\XSLTFile1.xslt");

             XmlDocument paramDoc = MakeParamDoc(items);

             XsltArgumentList args = new XsltArgumentList();
             args.AddParam("foo", "", paramDoc);

             xsltProc.Transform(XmlReader.Create(new 
StringReader("<dummy/>")), args, Console.Out);
             Console.WriteLine();
             Console.WriteLine("Number of item elements: {0}.", 
paramDoc.SelectNodes("/root/item").Count);
             Console.WriteLine();
             Console.WriteLine();
         }

         static XmlDocument MakeParamDoc(IEnumerable<string> items)
         {
             XmlDocument paramDoc = new XmlDocument();
             using (XmlWriter writer = 
paramDoc.CreateNavigator().AppendChild())
             {
                 writer.WriteStartElement("root");
                 foreach (string item in items)
                 {
                     writer.WriteElementString("item", item.ToString());
                 }
                 writer.WriteEndDocument();
             }
             return paramDoc;
         }
     }

And the output of that code is fine:

<?xml version="1.0" encoding="ibm850"?><result>5</result>
Number of item elements: 5.


<?xml version="1.0" encoding="ibm850"?><result>1</result>
Number of item elements: 1.


<?xml version="1.0" encoding="ibm850"?><result>0</result>
Number of item elements: 0.

As you can see both the stylesheet with XPath count as well as 
SelectNodes().Count yield the same result.


So to find out why your code does not give the result you are looking 
for we need to look at details of your code. Try to reduce the code 
(XSLT and C#) to a minimum to allow us to reproduce the problem, then 
post it.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent