inserting a new attribute to an existing element

Here should go questions about transforming XML with XSLT and FOP.
ameyzzin
Posts: 8
Joined: Tue Feb 15, 2005 12:00 am

inserting a new attribute to an existing element

Post 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>
ameyzzin
Posts: 8
Joined: Tue Feb 15, 2005 12:00 am

Got it

Post by ameyzzin »

thanks,
I got what i needed
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post 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
Post Reply