Altova Mailing List Archives

Re: How to replace attribute name

From: Martin Honnen <mahotrash@-----.-->
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=""

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

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

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

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


An example XML file

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

is transformed to

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

	Martin Honnen


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 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.