Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: XSL:Count on a passed parameter [Thread Next] Re: XSL:Count on a passed parameterTo: 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/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
