Re: [xsl] Xalan attribute order

Subject: Re: [xsl] Xalan attribute order
From: "Helder da Rocha" <helder@xxxxxxxxxxxxxxxx>
Date: Wed, 28 Aug 2002 16:04:41 -0300
Hi Thomas,

You wrote:

> I know, attribute order doesn't matter, and if order matters, something is
> wrong. But I generate HTML from XML and there are old tools. So I have to
> keep the attribute order. I guess Xalan sorts the attributs
alphabetically.
> Is there a way, to change the behaviour of Xalan?

Order does not matter and XML-compatible clients should not fail because of
attribute order. But you CAN control attribute order when you generate the
output document statically using Xalan. Attributes copied (using <xsl:copy>
or <xsl:copy-of>) from the source tree are output in the same order. Also
attributes created using <xsl:attribute> are output in the order they appear
in your template. The only exception to this rule is when the tag with
attributes is present in the template.

Consider the following XML document:

<phone country="55" area="11" number="87654321" />

If you use <copy> or <copy-of>, they will be output in the same order
(unless you explicitly sort them as someone else observed). But if you place
the above tag inside a template, the output order will be:

<phone area="11" country="55" number="87654321" />

You can control this using <xsl:attribute>:

<phone>
    <xsl:attribute name="country">55</xsl:attribute>
    <xsl:attribute name="area">11</xsl:attribute>
    <xsl:attribute name="number">87654321</xsl:attribute>
</phone>

You can also place <xsl:copy-of> any place between the <xsl:attribute>s and
control where you place attributes copied from the source tree.

<xsl:template match="tel">
  <phone>
      <xsl:attribute name="country">55</xsl:attribute>
      <xsl:copy-of select="@*" />
      <xsl:attribute name="area">11</xsl:attribute>
      <xsl:attribute name="number">87654321</xsl:attribute>
  </phone>
</template>

Will result in

<phone country="55" operator="21" range="international" area="11"
number="87654321" />

if source-document contains

<tel operator="21" range="international" />

Helder.

--
Helder da Rocha (helder@xxxxxxxxxxxxxxxx)
Web Consultant
www.argonavis.com.br
São Paulo, Brazil
+55 (11) 9291 0567



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


Current Thread