xsl-list
[Top] [All Lists]

RE: Perpetuating xsl instructions

2003-05-12 15:14:17
I have a need that requires appending the contents of 
multiple xml documents
at the end of a single style sheet - the same style sheet 
that is making the
transformations. So as this style sheet transforms the first 
xml document,
it needs to retain its instruction for the next xml.

Here is my style sheet (these lines need to be present for each
transformation):
<xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>
    
    <xsl:template match="*">
        <xsl:copy-of select="."/>
        <!-- appended xml documents go (grow) here. -->
    </xsl:template>

Do you really want your appended xml documents to go into the
template that matches "*"??
In other words do you want the result to be a stylesheet that
will output all the so-far-appended XML documents once for every
element of the new source document?

Maybe you really want the XML documents to be inserted somewhere
else, perhaps after the end of the last template.
(As your other email suggests.)

Maybe it would help if you explain your purpose... what are you trying
to achieve by accumulating XML documents in your stylesheet?  What do you
want to do with the stylesheet after you've run several xml documents
through it?

Here is a stylesheet that does most of what (I think) you want:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:my="http://www.my-dummy-namespace.org/lars/dummy"; >
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  
  <!-- self-replicating stylesheet:
    When run on a source file, this stylesheet should produce itself, with the
    contents of the source file inserted into it at a certain point. -->

  <xsl:template match="/">
    <xsl:apply-templates select="document('')/*">
      <xsl:with-param name="source-doc" select="/" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="xsl:stylesheet">
    <xsl:param name="source-doc" />
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
      <my:doc>
        <xsl:copy-of select="$source-doc" />
      </my:doc>
    </xsl:copy>
  </xsl:template>
 
  <!-- XML docs get inserted here -->

</xsl:stylesheet>


It's not a perfect replica -- you get a lot of extra whitespace
with each transformation.

The reason the XML docs get put into a <my:doc> element is
that if you put a namespaceless top-level element into a stylesheet,
you get an error.

Lars


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list