Re: [xsl] XML elments order

Subject: Re: [xsl] XML elments order
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 10 Dec 2001 10:50:04 +0000
Hi,

> I'm trying to convert an XML document into another XML document and
> I need to keep tags in the same order has they appear in the first
> document.

Probably you are telling the processor to first process the names, and
then process the content, using:

  <xsl:for-each select="name">
    ...
  </xsl:for-each>
  <xsl:for-each select="content">
    ...
  </xsl:for-each>

If you want to process them in document order, you have to tell the
processor to process them all, and then use something inside the
xsl:for-each to work out what kind of element's being processed:

  <xsl:for-each select="name | content">
    <xsl:choose>
      <xsl:when test="self::name">
        ...
      </xsl:when>
      <xsl:when test="self::content">
        ...
      </xsl:when>
    </xsl:choose>
  </xsl:for-each>

As you can see, that would get tedious if you had lots of different
kinds of elements, and difficult to manage if the elements nested
inside each other. Mostly, therefore, for this kind of processing, a
push approach is a better approach. In the push approach, you tell the
processor to apply templates to all the nodes:

  <xsl:apply-templates />

And then you have a number of templates that match the different kinds
of nodes and do something with them, so one template for names and one
template for content elements:

<xsl:template match="name">
  ...
</xsl:template>

<xsl:template match="content">
  ...
</xsl:template>
  
I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread