xsl-list
[Top] [All Lists]

RE: Multiple tranformations of a given element using XSL

2002-12-11 02:29:15

To make it more clear, after I change an element name
during the first pass, if I would like to change the
content of the element during the second pass, the
second template is working on the source tree (which
is never modified) and hence the first change is being 
compromised on the output tree. If there is a way I can make 
the second template work on the DOM modified by the first 
template, then I can probably use the new element name in the 
'match' variable of the second template.

In essence, the first set of templates will modify the
DOM during the first pass, the modified DOM will be
the source tree for the second set of templates and so
on.


Yes, it's perfectly possible to write multi-pass transformations, and
it's often a good way of keeping your stylesheets well-structured and
modular, even in cases where a single-pass solution is feasible.

There are two approaches: using multiple transformations with multiple
stylesheets, and using a single transformation with multiple phases
within a single stylesheet.

In the first approach you control the sequence of transformations from
the processor's API. This is particularly convenient to do using JAXP in
the Java world, but it's not difficult with Microsoft's API either.

In the second approach you need to use a temporary tree and the
xx:node-set extension. The typical logic is:

<xsl:template match="/">
  <xsl:variable name="temp">
    <xsl:apply-templates select="/" mode="phase1"/>
  </xsl:variable>
  <xsl:apply-templates select="xx:node-set($temp)" mode="phase2"/>
</xsl:template>

You can of course implement the two modes (phases) in different
stylesheet modules if you wish.

Another approach is Saxon's "next-in-chain" extension which allows one
stylesheet to nominate another stylesheet that is used to process the
result tree from the first stylesheet.

Incidentally, it's probably best not to use the term DOM to refer to
trees in the XPath data model: there are many differences between the
DOM model and the XPath model. Microsoft blur the distinction by
implementing both models in one product.

Michael Kay
Software AG
home: Michael(_dot_)H(_dot_)Kay(_at_)ntlworld(_dot_)com
work: Michael(_dot_)Kay(_at_)softwareag(_dot_)com 


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