Re: [xsl] Enforcing element order

Subject: Re: [xsl] Enforcing element order
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 02 May 2007 22:51:49 +0200
ms wrote:
Hi:

I have an xml input file as shown below:

Your XML is not well-formed. It usually helps to write XML with some XML editor and test it with your XSLT before sending it, and showing what output you currently get, and what you'd rather have instead.




As you can see elements image, table and text can
appear before and
after <l1>. Now in the PDF, I want all image, text and
table elements
to appear before l1 and the ones after to appear after
l1.

So basically, you want the element order to stay the same as the input order. The short answer is: you don't need to do anything.


Here is my XSLFO piece for tjis part:

You showed XSLT , not XSL-FO.


<xsl:choose>
<xsl:when test="*[self: :image[following -sibling:
:table] or
self::image[ following- sibling:: text] or
self::image] ">
<xsl:apply-template s select="*[not( self::image) and
not(self::table[ preceding: :image]) and
not(self::text[ preceding: :image])] "/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-template s/>
</xsl:otherwise>
</xsl:choose>


That is not valid XSLT. Please test your XSLT before sending it. Your code shows that it'd help you to find a good tutorial on the basics of XSLT processing. Using xsl:when in this way is unnecessary and superfluous.


There are several ways to solve your problem, but basically, there is no problem at all: XSLT will do what you want automatically, though here's one:

<xsl:template match="/">
  <xsl:apply-templates select="mfunc/*" />
</xsl:template>

<xsl:template match="l1">
   ... do something with l1...
   <xsl:apply-templates />
</xsl:template>

<xsl:template match="image">
   ... do something with image ...
   <xsl:apply-templates />
</xsl:template>

<xsl:template match="text">
   ... do something with text ...
   <xsl:apply-templates />
</xsl:template>


Note that the order of the xsl:template are unimportant. The output will look like you want: the elements before <l1> are processed before <l1> and the elements after <l1> are processed thereafter. And so will your output.




Can anyone suggest a different apprach to this
problem?

Yep, see above ;)


Cheers,
-- Abel Braaksma

Current Thread