Re: [xsl] output the result of the transformation twice, indented and not indented, without duplicating the code

Subject: Re: [xsl] output the result of the transformation twice, indented and not indented, without duplicating the code
From: "Graydon graydon@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 23 May 2022 19:01:21 -0000
On Mon, May 23, 2022 at 06:39:30PM -0000, Wolfhart Totschnig wolfhart.totschnig@xxxxxxxxxxx scripsit:
> I have an XSL stylesheet that produces a large XML document. I would like to
> output the XML document twice, once indented (for human readability) and
> once not indented (for faster access by other scripts). I am wondering
> whether this can be done from within a single stylesheet.

Sure; you store the result document in a variable, minimally:

<xsl:template name="xsl:initial-template">
  <xsl:variable name="result">
    <xsl:apply-templates select="/node()"/>
  </xsl:variable>

  <xsl:result-document href="$path-for-humans" format="main" indent="true">
    <xsl:sequence select="$result"/>
  </xsl:result-document>
  <xsl:result-document href="$path-for-machines" format="main" indent="false">
    <xsl:sequence select="$result"/>
  </xsl:result-document>

</xsl:template>

<xsl:output name="main" /> has all the other things like doctype or
encoding or method you would want to define for the result documents, so
you can change them in one place.

This works in XSLT 2 or XSLT 3; it won't work in 1.

The second result document can just be xsl:sequence and will emit the
variable as the transform's result document, but that way you can't use
the single named output to control everything but the indent for both
result documents.

-- 
Graydon Saunders  | graydonish@xxxxxxxxx
^fs oferiode, pisses swa mfg.
-- Deor  ("That passed, so may this.")

Current Thread