Is it possible to apply a template to the result of a call-template?
For example, the xml and 2 xslt below.
index.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<book>
<title>Ethics</title>
</book>
<book>
<title>Beyond Good and Evil</title>
</book>
</root>
index2.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/" name="temp2">
<foo>
<xsl:for-each select="//book">
<bar><xsl:value-of select="."/></bar>
</xsl:for-each>
</foo>
</xsl:template>
</xsl:stylesheet>
and index.xsl is calling index2.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:include href="index2.xsl" />
<!-- apply a template to the <xsl:call-template name="temp2" /> result -->
</xsl:stylesheet>
Is there any way to apply a template to the result of <xsl:call-template name="temp2" />
in index.xsl?
Thanks in advance.
Yes, this is called multi-pass processing. The only peculiarity is that in XSLT 1.0 you must apply the implementation-dependent
xxx:node-set()
extension function to any intermediate result, which in general will be of the infamous RTF (Result Tree Fragment) type and thus will need to be converted to a regular tree.Here is a complete example of multi-pass processing (I am using
xsl:apply-templates
and notxsl:call-template
, but you can freely modify this example to use named templates andxsl:call-template
instead):when this transformation is applied on the following XML document:
the wanted result ( the content of any
num
elements is doubled in the first pass, then squared in the second pass) is produced:II. In XSLT 2.0
The RTF "type" was abolished, so it is much more easier to specify multi-pass processing, which becomes almost indistinguishable from functional composition, as the following equivalent transformation shows:
when this transformation is applied on the same XML document (above), the same correct result is produced: