Efficient Stylesheets for reordering

Subject: Efficient Stylesheets for reordering
From: Jose Alberto Fernandez <JFernandez@xxxxxxxxxxx>
Date: Tue, 7 Nov 2000 15:03:54 -0800
Hi, I send this question originally to the XalanJ list, but I guess this is
a more appropriate forum.
I am trying to build templates that are suppose to reorder the nodes of an
For example, given "doc1" grammar, we want to obtain "doc2" defined as

<!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" />
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" />

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


Am I completely wrong on my understanding of XSLT?

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.

Are there any extensions that would allow discarding from memory those parts
of the original document that are no longer needed?

Now, I know that whether one gets actual streamming of the output depends on
the particular
implementation, but what I am interested here is on ways of writing the
stylesheet that will
not deminish the ability of an implementation to stream the output.

Hope you have some good ideas,

Jose Alberto

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

Current Thread