Reputation: 57
I have a requirement to count the number of nodes in an XML and use that value in the code to update the database. I am using XSLT to perform the transformation of the XML. Not sure If there is a way to access the node count variable outside the XSLT processor. I am handling big XML feeds of sizes over 3 GB
Upvotes: 0
Views: 59
Reputation: 163312
This kind of thing is going to depend on which XSLT processor you are using. If you're using Saxon, for example, you could write a stylesheet function
<xsl:function name="my:node-count" as="xs:integer">
<xsl:param name="doc"/>
<xsl:sequence select="count($doc//*)"/>
</xsl:function>
and invoke it from Java using Xslt30Transformer.callFunction()
.
But it would be simpler to bypass the XSLT entirely, and simply use a Java expression such as
doc.select(Steps.descendant("*")).count();
With a document size of 3Gb, you might be wanting to use XSLT streaming, but you don't mention that. Saxon's support for streaming is primarily via XSLT, and it's not well supported directly in the Java API. A good technique with streaming is to compute multiple results from a single pass of the input document, delivering the results as a map:
<xsl:source-document href="input.xml" streamable="yes">
<xsl:map>
<xsl:map-entry key="'primary-output'">
<xsl:apply-templates/>
</xsl:map-entry>
<xsl:map-entry key="'count'">
<xsl:sequence select="count(.//*)"/>
</xsl:map-entry>
</xsl:map>
</xsl:source-document>
This returns a map as the result of the transformation, and you can access the individual entries in the returned map from the Java API.
Upvotes: 1