You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Jose Alberto Fernandez <JF...@viquity.com> on 2000/10/19 00:49:43 UTC

Best techniques for reordering templates

Hi,
 
I am trying to build templates that are suppose to reorder the nodes of an
input.
For example, given "doc1" grammar, we want to obtain "doc2" defined as
follows
 
  <!ELEMENT doc1 (s1, s2, s3*, s4?, s5+) >
 
  <!ELEMENT doc2 (s1, s3*, s4?, s2, s5+) >
 
Now, the simplest way to do this is with a template like:
 
<xsl:template match="doc1" >
  <xsl:apply-template select="s1" />
  <xsl:apply-template select="s3" />
  <xsl:apply-template select="s4" />
  <xsl:apply-template select="s2" />
  <xsl:apply-template select="s5" />
</xsl:template>

 
plus other templates that just copy the "s*" nodes just as they are.
 
Now, such template will wait until the whole document is read
before generating anything, because it does not know how many "s1"
can be in "doc1". One could inprove it be doing things like:
 
<xsl:template match="doc1" >
  <xsl:apply-template select="s1[1]" />
  <xsl:apply-template select="s3" />
  <xsl:apply-template select="s4[1]" />
  <xsl:apply-template select="s2[1]" />
  <xsl:apply-template select="s5" />
</xsl:template>

 
Here since we only need the first "s1" (it will be only one) we can start
generating output as soon as possible.
And start generating "s3"s as we see them, but for "s3" and "s4" we will
have to wait until
the end of the document, if they are not actually present (they are
optional).
 
Questions:
(1) Would Xalan2J do this early generation given the optimizations presented
above?
 
(2) Is there some other way to write the template so that we can just stream
out most of
the work? Notice that the only thing is needed to perform the transformation
is to
hold to the "s2" node as it is read, and put it back on the output just
before generating
the first "s5". The rest of the nodes can be just copied as they are from
input to output.
 
Hope you have some good ideas,
 
Jose Alberto