Page 1 of 1

inserting a new attribute to an existing element

Posted: Tue Feb 15, 2005 12:10 am
by ameyzzin
Hi,
I have an xml file , which has several elements ..i would like to add a new attribute to an existing element.
eg. if i wan to add attribute "district" to element "Center"
<xsl:attribute name"District">45</xsl:attribute>
and write the xml file again with updated attribute.
Thanks

Code: Select all


<Dispatch Data="16" Ver="1.5">
<Center No="2124" cc="US" region="35" Code="1" numBreaks="0" ShipNum="" GMT="0"/>
<DispDetails SerialNumber="8SZVB221" totalPkgs="5" >
<CurrencyID>L1379229754</CurrencyID>
</DispDetails>
</Dispatch>

Got it

Posted: Tue Feb 15, 2005 1:32 am
by ameyzzin
thanks,
I got what i needed

Posted: Tue Feb 15, 2005 12:07 pm
by george
Hi,

You can do that with something like:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Center">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="District">45</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George