rahul
rahul

Reputation: 1122

XSLT remove ns from XML

Struggling generating expected XML using XSLT in which I am trying to achieve versioning of XML and want to remove id tag from the XML. XSLT remove tag but it added xmlns="" as an attribute of each tag in the output.

XML looks like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer xmlns="urn:com:xmlschema:service:v1.1:Address.xsd">
    <ID>123</ID>
    <Status>PRESENT</Status>
    <InternetAddress>00:00:00:00:00</InternetAddress>
    <PhysicalAddress>SOME ADDRESS</PhysicalAddress>
    <FamilyMember>8</FamilyMember>
</Customer>

XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="urn:com:xmlschema:service:v1.1:Address.xsd"
                exclude-result-prefixes="#all" >
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="*">
        <xsl:element name="{local-name(.)}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name(.)}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="ns:Customer">
        <Customer xmlns="urn:com:xmlschema:service:v2.1:Address.xsd">
            <xsl:apply-templates select="@*|node()"/>
        </Customer>
    </xsl:template>
    <xsl:template match="ns:ID"/>
</xsl:stylesheet>

Expected output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer xmlns="urn:com:xmlschema:service:v2.1:Address.xsd">
    <Status>PRESENT</Status>
    <InternetAddress>00:00:00:00:00</InternetAddress>
    <PhysicalAddress>SOME ADDRESS</PhysicalAddress>
    <FamilyMember>8</FamilyMember>
</Customer>

XSLT generated

<?xml version="1.0" encoding="UTF-8"?>
<Customer xmlns="urn:com:xmlschema:service:v2.1:Address.xsd">
   <Status xmlns="">PRESENT</Status>
   <InternetAddress xmlns="">00:00:00:00:00</InternetAddress>
   <PhysicalAddress xmlns="">SOME ADDRESS</PhysicalAddress>
   <FamilyMember xmlns="">8</FamilyMember>
</Customer>

xmlns="" I want to remove. Kindly suggest where I am committing a mistake.

Upvotes: 1

Views: 334

Answers (1)

zx485
zx485

Reputation: 29042

You were trying to remove the namespaces of the children of your root element. That worked, hence the xmlns=''. But the xmlns=... sets a default namespace for all of its children - which you were trying to cancel in your first two templates.

To get the output you want you can get rid of these templates and just use the removal template with a template that changes the namespace of all elements to the new one:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="urn:com:xmlschema:service:v1.1:Address.xsd" exclude-result-prefixes="#all" >
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="ns:*">
        <xsl:element name="{local-name()}" namespace="urn:com:xmlschema:service:v2.1:Address.xsd">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="ns:ID"/>
</xsl:stylesheet>

Its result is as desired (supposedly):

<?xml version="1.0" encoding="UTF-8"?>
<Customer xmlns="urn:com:xmlschema:service:v2.1:Address.xsd">
   <Status>PRESENT</Status>
   <InternetAddress>00:00:00:00:00</InternetAddress>
   <PhysicalAddress>SOME ADDRESS</PhysicalAddress>
   <FamilyMember>8</FamilyMember>
</Customer>

This even works in XSLT-1.0.

Upvotes: 2

Related Questions