xsl-list
[Top] [All Lists]

Re: sorting AND copying of XML via XSL

2003-09-09 11:02:01
Use the identity template and override it for nodes that have "bar" or
"item" children like this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:strip-space elements="*"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="node()[bar or item]">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates>
        <xsl:sort select="@id" data-type="number"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>


When this transformation is applied on your source.xml:

<foo>
  <bar id="2">
    <item id="6">hello</item>
    <item id="4">world</item>
  </bar>
  <bar id="1">
    <item id="7">how's</item>
    <item id="3">life</item>
  </bar>
</foo>

the wanted result is produced:

<foo>
   <bar id="1">
      <item id="3">life</item>
      <item id="7">how's</item>
   </bar>
   <bar id="2">
      <item id="4">world</item>
      <item id="6">hello</item>
   </bar>
</foo>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL



"marcel salathe" <ms7141(_at_)gmx(_dot_)de> wrote in message
news:5295(_dot_)1063122940(_at_)www25(_dot_)gmx(_dot_)net(_dot_)(_dot_)(_dot_)
Hi

I have a XSL question which might be easy to answer, but I couldnt.

Suppose I have an XML document like this:

<foo>
  <bar id="2">
    <item id="6">hello</item>
    <item id="4">world</item>
  </bar>
  <bar id="1">
    <item id="7">how's</item>
    <item id="3">life</item>
  </bar>
</foo>

Now I want to sort the bar elements and then the item elements according to
their Id's, and finally output
the entire document sorted, e.g.

<foo>
  <bar id="1">
    <item id="3">life</item>
    <item id="7">how's</item>
  </bar>
  <bar id="2">
    <item id="4">world</item>
    <item id="6">hello</item>
  </bar>
</foo>

I could achieve sorting or copying, but I couldnt do both together. I guess
it must be quite a simple
XSLT.

Any help very much appreciated.

Best regards,
marcel


-- 
COMPUTERBILD 15/03: Premium-e-mail-Dienste im Test
--------------------------------------------------
1. GMX TopMail - Platz 1 und Testsieger!
2. GMX ProMail - Platz 2 und Preis-Qualitätssieger!
3. Arcor - 4. web.de - 5. T-Online - 6. freenet.de - 7. daybyday - 8. e-Post


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





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



<Prev in Thread] Current Thread [Next in Thread>