Forums Register Login

Difference between count() and last()

+Pie Number of slices to send: Send
In XPath, is there any difference between the count() and last() functions? If you had a nodeset of 3 elements they would both return the value 3, correct? It's just that count() returns the number of nodes and last() returns the position of the last node. But, wouldn't these always be the same, or is there something more subtle that I am missing?
+Pie Number of slices to send: Send
Jeff,
Have a look at the xml file and the following stylesheet -
===============================================
<!-- XML document -->
<source>
<AAA>
<BBB>
<CCC>Carl</CCC>
</BBB>
<BBB/>
<BBB/>
</AAA>
<AAA>
<BBB/>
<BBB>
<CCC attr="1">John</CCC>
<CCC attr="2">Charles</CCC>
<CCC>Robert</CCC>
<CCC>Anthony</CCC>
</BBB>
</AAA>
</source>
=======================================
<!-- XSL stylesheet -->
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:call-template name="showDifference"/>
</xsl:template>
<xsl:template name="showDifference">
<p>
<xsl:text> Count of all CCC elements ::</xsl:text>
<xsl:value-of select ="count(//CCC)"/>
</p>
<p>
<xsl:text> Count of all CCC elements with attribute "attr" ::</xsl:text>
<xsl:value-of select ="count(//CCC[@attr])"/>
</p>
<xsl:apply-templates select="//AAA[last()]//BBB[last()]//CCC"/>
</xsl:template>
</xsl:stylesheet>
========EXPLANATION FOLLOWS===============
1) The first difference is that count( ) requires a nodeset as input. The above xml file has 5 CCC elements in total and you can see the output by running it thru an xslt processor
2) last( ) function returns the size of the current context. If we look at the xpath expression in the above example,
select="//AAA[last()]//BBB[last()]//CCC"/>
This means that select all the child CCC elements, of the last of the BBB child of the last AAA parent.
3) count ( ) has nothing to do with the context and just returns the no. of nodes that satisfy a given xpath expression; hence the expression -
<xsl:value-of select ="count(//CCC[@attr])"/>
returns a value of 2 as there are two CCC nodes
in the xml document with "attr" as the attribute name.
I hope that this answers your question
+Pie Number of slices to send: Send
Thanks for the very clear explanation. That makes perfect sense.
+Pie Number of slices to send: Send
As the big book says (XML in a nutshell) -
The last() function returns the number of nodes in the context node set, which is the same as the position of the last node in the set. The count() function is similar, except that it returns the number of nodes in its node set argument rather than in the context node list.
Cheers,
Dan
Trust God, but always tether your camel... to this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1142 times.
Similar Threads
Combining fields from 2 resultsets in 1 vector
Olympic 2008
A doubt about Xpath expression
Difference between run and start method of Thread
Objects instance variables value
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:17:02.