3

Is it possible to store the output of an XSL transformation in some sort of variable and then perform an additional transformation on the variable's contents? (All in one XSL file)

(XSLT-2.0 Preferred)

deken
  • 387
  • 2
  • 3
  • 12

2 Answers2

9

XSLT 2.0 Solution :

<xsl:variable name="firstPassResult">
  <xsl:apply-templates select="/" mode="firstPass"/>
</xsl:variable>

<xsl:template match="/">
  <xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
</xsl:template>

The trick here, is to use mode="firstPassResult" for the first pass while all the templates for the sedond pass should have mode="secondPass".

Edit:

Example :

<root>
  <a>Init</a>
</root>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="firstPassResult">
    <xsl:apply-templates select="/" mode="firstPass"/>
  </xsl:variable>

  <xsl:template match="/" mode="firstPass">
      <test>
        <firstPass>
          <xsl:value-of select="root/a"/>
        </firstPass>
      </test>
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
  </xsl:template>

  <xsl:template match="/" mode="secondPass">
    <xsl:message terminate="no">
      <xsl:copy-of select="."/>
    </xsl:message>
  </xsl:template>

</xsl:stylesheet>

Output :

[xslt] <test><firstPass>Init</firstPass></test>

So the first pass creates some elements with the content of root/a and the second one prints the created elements to std out. Hopefully this is enough to get you going.

FailedDev
  • 26,680
  • 9
  • 53
  • 73
  • I'm having trouble figuring out what goes where, can you show a quick example with this solution? Thanks. – deken Nov 21 '11 at 17:39
1

Yes, with XSLT 2.0 it is easy. With XSLT 1.0 you can of course also use modes and store a temporary result in a variable the same way as in XSLT 2.0 but the variable is then a result tree fragment, to be able to process it further with apply-templates you need to use an extension function like exsl:node-set on the variable.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110