Reputation: 6831
Normally, the XSLT should work on an original XML file and being provided with a stylesheet.
But currently my task is, my last node in my final XML file should be a number that count certain types of nodes in my RESULTING xml file (not the original one).
So for example, my initial XML is empty, and in my style sheet, I've grabbed data from some other places and inserted them into this empty XML to create certain nodes like this:
<Plant>
<Flower>
<Flower1>..</Flower1>
<Flower2>..</Flower2>
</Flower>
<Tree>
<Tree1>...</Tree1>
<Tree2>...</Tree2>
</Tree>
....
<Counter>? </Counter>
</Plant>
My question here is this Counter node, it should be a number that indicates how many children nodes of Plant has been created after the transformation. So I would assume a 2-step process could be effective: The first step is to do an intermediate XSLT that grabs all the "Plant" data and filled in the empty initial XML file; Then the second step is to a simple XPath count on this XML file and append another node "Counter" to contain this number. But I am not exactly sure how to do chain these two XSLTs into a single XSLT( since a one-click transform is required), like how to represent the intermediate XML files and how to command the IDE( XMLSpy in my case) to do a further XSLT.
Thanks in advance.
Upvotes: 3
Views: 379
Reputation: 243479
Here is a general way to process the result of a transformation, including counting its nodes:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:output method="text"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<top>
<xsl:apply-templates select="*"/>
<xsl:apply-templates select="*"/>
</top>
</xsl:variable>
<xsl:variable name="vPass2" select="ext:node-set($vrtfPass1)"/>
<xsl:value-of select="count($vPass2/*/*/*)"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<nums>
<num>1</num>
<num>2</num>
<num>3</num>
<num>4</num>
<num>5</num>
</nums>
the correct result is produced:
10
In XSLT 1.0 (only) the use of the xxx:node-set()
extension function is generally required in multi-pass processing. THere is no such requirement in XSLT 2.0, which eliminated the infamous RTF "datatype".
Upvotes: 4