Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


why is count(xpath) != xpath[last()] ?

From: jfrensen@-------.---
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.


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