How to include output of one XSLT file in another?

1.1k Views Asked by At

Is there any way of server side include XSLT file?
I mean that the output of one file is shown in the second file?

For example:

Main XSLT file:

<xsl:template match="pt">
  <xsl:comment>#include virtual="a.xsl"</xsl:comment>
</xsl:template>

Second XSLT file a.xslt:

<xsl:template match="a">
  <xsl:variable name="ab" select="'ss'"/>
   <p><xsl:value-of select="$ab"/></p>
</xsl:template>

Now output will be

ss in p tag

Why I am doing it like this is because I break my pages into multiple components after that I include their output in the main XSLT file. Each XSLT file has individual XML data.

1

There are 1 best solutions below

0
On

You can't use the output of an XSLT stylesheet as input to transform it again in vanilla XSLT 1.0 in a single stylesheet.

You either need to run the XSLT processor twice, using the result of the first pass as input for the second, or you need to use a processor that supports something like node-set(), which allows to convert a result tree fragment back to a node-set for further processing.

https://www.xml.com/pub/a/2003/07/16/nodeset.html

You can include arbitrary XML in the output however, as long as it is valid XML, using the document() function.

stylesheet.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <externalData>
      <xsl:copy-of select ="document('some.xml')"/>
    </externalData>
  </xsl:template>

</xsl:stylesheet>

some.xml

<?xml version="1.0" encoding="utf-8"?>
<content>
  <foo>bar</foo>
</content>

Result

<?xml version="1.0" encoding="utf-8"?>
<externalData>
  <content>
    <foo>bar</foo>
  </content>
</externalData>