xsl-list
[Top] [All Lists]

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

2022-05-23 14:01:26
On Mon, May 23, 2022 at 06:39:30PM -0000, Wolfhart Totschnig 
wolfhart(_dot_)totschnig(_at_)mail(_dot_)udp(_dot_)cl 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(_at_)gmail(_dot_)com
Þæs oferéode, ðisses swá mæg.
-- Deor  ("That passed, so may this.")
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>