Altova Mailing List Archives


Re: How to replace attribute name

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 4/28/2004 12:47:00 PM

Alison wrote:

> I have an xml file. In each node, there are 2 attributes.
> What I want to do is to replace the first attribute name with a new
> name and delete the other attribute and its value. Can someone help
> to give me a clue or sample code ?

Attributes are not ordered thus speaking of the first and second 
attribute is not a good way to identify attributes, you need to go by 
the attribute name.
If you know the names then the task is simple, start with the identity 
transformation and add two templates for the two attributes, one 
changing the name of the attribute, the other one being empty to do 
nothing so that the attribute is not copied:

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

<xsl:output method="xml" encoding="UTF-8" />

<xsl:template match="@att2" />

<xsl:template match="@att1">
   <xsl:attribute name="attribute"><xsl:value-of select="." 
/></xsl:attribute>
</xsl:template>

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

</xsl:stylesheet>

An example XML file

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <element att1="1" att2="1">
     <element att1="1, 1" att2="1,1" />
   </element>
   <element att1="2" att2="2" />
</root>

is transformed to

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <element attribute="1">
     <element attribute="1, 1"/>
   </element>
   <element attribute="2"/>
</root>
-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.