[xsl] XML tags to XML attributes with XSL

Subject: [xsl] XML tags to XML attributes with XSL
From: lbertin@xxxxxxxxxxxx
Date: Wed, 22 Oct 2003 09:48:55 +0200
Hello to the list !

I'm having a problem with XSL, i've done a lot of searching to find
the solution but it seems i'm stuck now. I hope you'll help me out
with the problem.

Here it goes, i have xml data which can contains tags like this

<Attributes>
<nom>toto</nom>
<valeur>123</valeurs>
</Attributes>

I want to parse it and make it become attributes of the parent node.
Which means, if the parent node is <Process> i will get :

<Process toto="123">

Here is an XML exemple and the xsl i've done with the result it
provides.

<?xml version="1.0" encoding="iso-8859-9"?>
<Message id="14245" name="ACCEPTTASKS" userid="bertin" dateemission
="15/10/2003">
<Process>
<Attributes>
<nom>id2</nom>
<valeur>4321</valeur>
</Attributes>
<Task>
<Attributes>
<nom>id</nom>
<valeur>1243</valeur>
</Attributes>
</Task>
</Process>
</Message>

Here is my XSL :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl
="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" encoding="ISO-8859-1" indent="yes"/>

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

<xsl:template match="Attributes">
     <xsl:variable name="nomattr" select="nom"/>
     <xsl:attribute name="{$nomattr}">
          <xsl:value-of select="valeur"/>
     </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

XML Result :

<?xml version="1.0" encoding="ISO-8859-1"?>
<Message>
<Process id2="4321">
<Task id="1243"/>
</Process>
</Message>

As you can see, my attributes of <Message> have been stripped. I'm pretty
sure there is a more proper way to do what i want but i cant find it.

Any help would be very much appreciated !

Thanks a lot !

Laurent Bertin


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


Current Thread