New to xsl / xml - reorder issue

Here should go questions about transforming XML with XSLT and FOP.
sono
Posts: 2
Joined: Wed Jan 31, 2007 8:50 am
Contact:

New to xsl / xml - reorder issue

Post by sono »

Hello all,

I am new to xml and xsl and am trying to reorder elements in an xml file to be output as an xml file that is being placed into Adobe InDesign.

My xml file to be reordered is written as such:

Code: Select all


<advertisement>
<tester>
<three>Coloumn three</three>
<one>Column one</one>
<two>Column two</two>

</tester>
<tester>
<three>Second Coloumn three</three>
<one>Second Column one</one>
<two>Second Column two</two>

</tester>
<tester>
<three>Third Coloumn three</three>
<one>Third Column one</one>
<two>Third Column two</two>

</tester>
<tester>
<three>Fourth Coloumn three</three>
<one>Fourth Column one</one>
<two>Fourth Column two</two>

</tester>
<tester>
<three>Fifth Coloumn three</three>
<one>Fifth Column one</one>
<two>Fifth Column two</two>

</tester>
<tester>
<three>Sixth Coloumn three</three>
<one>Sixth Column one</one>
<two>Sixth Column two</two>

</tester>
</advertisement>
My xsl that I wrote looks like this:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:element name="advertisement">
<xsl:element name="tester">
<!--Rearrange the order of the elements-->
<xsl:copy-of select="advertisement/tester/one"/>
<xsl:copy-of select="advertisement/tester/two"/>
<xsl:copy-of select="advertisement/tester/three"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
What I get is this:

Code: Select all


<?xml version="1.0" encoding="UTF-16"?>
<advertisement>
<tester>
<one>Column one</one>
<one>Second Column one</one>
<one>Third Column one</one>
<one>Fourth Column one</one>
<one>Fifth Column one</one>
<one>Sixth Column one</one>
<two>Column two</two>
<two>Second Column two</two>
<two>Third Column two</two>
<two>Fourth Column two</two>
<two>Fifth two</two>
<two>Sixth Column two</two>
<three>Coloumn three</three>
<three>Second Coloumn three</three>
<three>Third Coloumn three</three>
<three>Fourth Coloumn three</three>
<three>Fifth three</three>
<three>Sixth Coloumn three</three>
</tester>
</advertisement>
What I would like is this:

Code: Select all


<advertisement>
<tester>
<one>Column one</one>
<two>Column two</two>
<three>Coloumn three</three>
</tester>
<tester>
<one>Second Column one</one>
<two>Second Column two</two>
<three>Second Coloumn three</three>
</tester>
<tester>
<one>Third Column one</one>
<two>Third Column two</two>
<three>Third Coloumn three</three>
</tester>
<tester>
<one>Fourth Column one</one>
<two>Fourth Column two</two>
<three>Fourth Coloumn three</three>
</tester>
<tester>
<one>Fifth Column one</one>
<two>Fifth Column two</two>
<three>Fifth Coloumn three</three>
</tester>
<tester>
<one>Sixth Column one</one>
<two>Sixth Column two</two>
<three>Sixth Coloumn three</three>
</tester>
</advertisement>


I would appreciate any advise.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

I would do this with a modified recursive copy stylesheet as below

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tester">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="one"/>
<xsl:apply-templates select="two"/>
<xsl:apply-templates select="three"/>
<xsl:apply-templates select="node()[not(self::one or self::two or self::three)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Regards,
George
George Cristian Bina
sono
Posts: 2
Joined: Wed Jan 31, 2007 8:50 am
Contact:

THANKYOU!

Post by sono »

:!: :!: :!: Thank you George :!: :!: :!: :!:

Thank you so much for your help!
Post Reply