Re: [xsl] how to replace an XML tag with a result tree and parse it again

Subject: Re: [xsl] how to replace an XML tag with a result tree and parse it again
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 4 Jun 2001 12:24:53 +0100
Hi Janning,

> then i am in trouble, because the tree in $mytoc doesnt belong to
> the main xml tree. So here is my question: How can i achieve that
> templates replace something in the input tree and the Stylesheet
> behaves as it was always there?

Your best bet is to do a two-pass solution, where the first pass
creates the XML that you want to use as the source of the next pass.
These passes could be done by separate stylesheets, or you could do
them in the same stylesheet if you're prepared to use a node-set()
extension function (which I think you are, given how you're describing
addressing it at the moment).

Build a controlling template to manage the transformation.  I usually
use the root-node-matching template for this.  Within it, you want to
apply templates to the source as it stands in 'substitute' mode and
then apply templates to the result of that in the default mode:

<xsl:template match="/">
   <xsl:variable name="substituted">
      <xsl:apply-templates mode="substitute" />
   <xsl:apply-templates select="exsl:node-set($substituted)/*" />

Have your toc-matching template apply only in 'substitute' mode,
replacing the toc element with the content that you want it to

<xsl:template match="toc" mode="substitute">
      <listitem>Chapter 1</listitem>
      <listitem> Chapter 2</listitem>

For everything else in 'substitute' mode, you just want the identity

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

The rest of the templates can stay the same. The only things that will
differ between the source document and the one that you use as the
basis of the main part of the transformation is that the toc element
will be replaced and that the base URI of the document will be
different. The latter shouldn't matter unless you're using the
document() function with two arguments anywhere?

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread