JohnXsl
JohnXsl

Reputation: 269

Xslt change node and add namespace

I have a following XML

<?xml version="1.0"?>
<location>
<Destination>Des01</Destination>
<DesCode>ACD8701</DesCode>
<UniqueId>023154</UniqueId>
<Amount>26</Amount>
</location>

I want to change the <location> into <abc_ItemUpdate> and add namespace so the output should look like the following after using XSLT

<ns0:abc_ItemUpdate xmlns:ns0="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo">
    <ns0:Destination>Des01</ns0:LegalEntity>
    <ns0:DesCode>ACD8701</ns0:DesCode>
    <ns0:UniqueId>023154</ns0:UniqueId>
    <ns0:Amount>26</ns0:Amount>
</ns0:abc_ItemUpdate>

Thanks in advance

Upvotes: 2

Views: 931

Answers (1)

Emiliano Poggi
Emiliano Poggi

Reputation: 24826

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes"/>

 <xsl:template match="/location">

     <xsl:element name="ns0:abc_ItemUpdate" namespace="http://yournms">
         <!-- copy attributes if any -->
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates/>
     </xsl:element>

 </xsl:template>

 <xsl:template match="*">
     <xsl:element name="ns0:{name()}" namespace="http://yournms">
         <!-- copy attributes if any -->
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates/>
     </xsl:element>
 </xsl:template>

</xsl:stylesheet>

Upvotes: 2

Related Questions