Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries [Thread Prev] >Thread Next - Re: why is count(xpath) != xpath[last()] ? why is count(xpath) != xpath[last()] ?To: NULL Date: 12/12/2007 6:13:00 AM While writing a template, I came across a situation in Microsoft's evaluation of my XSLT where, for a given XPath expression, count() of that nodeset was not equal to last() of that nodeset. I can't find anything in my books, Jeni Tennison's e.g., that explains this behavior. I've reduced the code to the following trivial example. My XML input is <?xml version="1.0" encoding="UTF-8"?> <root> <Students> <row> <Name>Bob</Name> </row> <row> <Name>Joe</Name> </row> </Students> </root> and my XSLT is <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> <xsl:output method="xml" media-type="text/xml" encoding="UTF-8" /> <xsl:preserve-space elements="*" /> <xsl:variable name="countNames" select="count(/root/Students/row/ Name)" /> <xsl:variable name="allNames" select="/root/Students/row/Name" /> <xsl:variable name="firstName" select="/root/Students/row/ Name[position() = 1]" /> <xsl:variable name="lastName" select="/root/Students/row/ Name[position() = last()]" /> <xsl:variable name="firstName2" select="(/root/Students/row/Name) [position() = 1]" /> <xsl:variable name="lastName2" select="(/root/Students/row/Name) [position() = last()]" /> <xsl:template match="/"> <Students> <Count> <xsl:value-of select="$countNames" /> </Count> <AllNames> <xsl:copy-of select="$allNames"/> </AllNames> <Case1> <First> <xsl:value-of select="$firstName" /> </First> <Last> <xsl:value-of select="$lastName" /> </Last> </Case1> <Case2> <First> <xsl:value-of select="$firstName2" /> </First> <Last> <xsl:value-of select="$lastName2" /> </Last> </Case2> </Students> </xsl:template> </xsl:stylesheet> The XML output from this transform is <?xml version="1.0" encoding="utf-8"?> <Students> <Count>2</Count> <AllNames> <Name>Bob</Name> <Name>Joe</Name> </AllNames> <Case1> <First>Bob</First> <Last>Bob</Last> </Case1> <Case2> <First>Bob</First> <Last>Joe</Last> </Case2> </Students> Note that the XPath /root/Students/row/Name yielded a nodeset of two names, as shown by the value of both Count and AllNames. But the expression /root/Students/row/Name[position() = last()] did not evaluate to the second name. Even more strangely, /root/Students/row/Name[1] and / root/Students/row/Name[last()] yielded the same result, even though the nodeset in question contains two names. Can anyone explain this strange behavior? Thanks. | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
