inserting a new attribute to an existing element

Here should go questions about transforming XML with XSLT and FOP.
ameyzzin
Posts: 8

inserting a new attribute to an existing element

Tue Feb 15, 2005 12:10 am

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

Got it

Tue Feb 15, 2005 1:32 am

thanks,
I got what i needed
george
Site Admin
Posts: 2100

Tue Feb 15, 2005 12:07 pm

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

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 1 guest