Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Transform XML using XSLT based on element position

Reply
Thread Tools

Transform XML using XSLT based on element position

 
 
Igor
Guest
Posts: n/a
 
      07-20-2003
Is there any way to resort and xml document using xslt based on
element position.
For example if I have xml like this:
<root>
<element> 1st thing </element>
<element> 2nd thing </element>
<element> 3rd thing </element>
</root>

would it be possible using xslt only to reverse it into:
<root>
<element> 3rd thing </element>
<element> 2nd thing </element>
<element> 1st thing </element>
</root>

I also can't sort based on the alphabetical or numerical order of the
actual information between the element tags, only on the position of
the tags, ie I would like to reverse their order.
 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      07-20-2003

"Igor" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Is there any way to resort and xml document using xslt based on
> element position.
> For example if I have xml like this:
> <root>
> <element> 1st thing </element>
> <element> 2nd thing </element>
> <element> 3rd thing </element>
> </root>
>
> would it be possible using xslt only to reverse it into:
> <root>
> <element> 3rd thing </element>
> <element> 2nd thing </element>
> <element> 1st thing </element>
> </root>


Yes, this is straightforward. This transformation:

<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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="root">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="position()"
order="descending"
data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<root>
<element> 1st thing </element>
<element> 2nd thing </element>
<element> 3rd thing </element>
</root>

produces the wanted result:

<root>
<element> 3rd thing </element>
<element> 2nd thing </element>
<element> 1st thing </element>
</root>




>
> I also can't sort based on the alphabetical or numerical order of the
> actual information between the element tags, only on the position of
> the tags, ie I would like to reverse their order.



Sorry, but I cannot understand what you mean here.


=====
Cheers,

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform XML to XML using XSLT adi XML 1 06-06-2006 01:06 AM
Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file jkflens XML 2 05-30-2006 09:41 AM
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM
How to use XSLT to transform XML according to the data in another XML ai2003lian@yahoo.com XML 1 02-02-2005 05:07 PM
How to use XSLT to transform XML according to the data in another XML ai2003lian@yahoo.com XML 0 02-02-2005 04:57 PM



Advertisments