xslt - Count the number of elements returned by <call-template> -


i have following xsl stylesheet:

<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/xsl/transform">   <xsl:output method="xml" encoding="utf-8"/>    <xsl:template match="/">     <xsl:variable name="elements">       <xsl:call-template name="get-some-nodes"/>     </xsl:variable>      <root>       <values>         <xsl:copy-of select="$elements"/>       </values>       <count>         <xsl:value-of select="count($elements)"/>       </count>     </root>   </xsl:template>    <xsl:template name="get-some-nodes">     <node>1</node>     <node>2</node>     <node>3</node>   </xsl:template>  </xsl:stylesheet> 

(it shouldn't matter xml apply to, generates own data).

the result of of (using xsltproc) is:

<?xml version="1.0" encoding="utf-8"?> <root xmlns="http://www.w3.org/1999/xhtml" xmlns:set="http://exslt.org/sets">   <values>     <node>1</node>     <node>2</node>     <node>3</node>   </values>   <count>1</count> </root> 

given called template returns 3 nodes, expected "count($elements)" 3, one. suspected maybe results being wrapped in sort of root node, attempt count($elements/*) or similar failed, believe because $elements result tree fragment, , not node-set.

i don't have access of goodies of exslt or xslt2.0, surely there's way count of nodes stored in variable?

i'd happy count nodes returned call-template without using intermediate variable, can't see how possible.

<xsl:variable name="elements">    <xsl:call-template name="get-some-nodes"/>  </xsl:variable>   <root>    <values>      <xsl:copy-of select="$elements"/>    </values>    <count>      <xsl:value-of select="count($elements)"/>    </count>  </root> 

in xslt 1.0, whenever nodes copied body of <xsl:variable>, contents of variable rtf (result-tree_fragment) , needs converted regular tree before further processing xpath.

an rtf can converted regular tree using extension function, ususally named xxx:node-set(), xxx prefix bound vendor-specific namespace.

to number of elements @ top level of tree, need:

count(xxx:node-set($elements)/*) 

here namespaces, xxx: bound:

"http://exslt.org/common/"  "urn:schemas-microsoft-com:xslt" 

in xslt 2.0 rtf "type" no longer exists , can have:

count($elements/*) 

if type of $elements isn't specified (the default document-node())

or

count($elements) 

if type of $elements specified element()*


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -