user1761848
user1761848

Reputation: 95

XSLT removing the prefix from the elements, except from the root node

How can I remove the prefix urn from the all elements, execept from the root node?

XML input

<urn:client xmlns:urn='www.testing.com' xmlns:x='http://schemas.xmlsoap.org/soap/envelope/'>
    <urn:header>
        <urn:desc1>undefined</urn:desc1>
        <urn:desc2>undefined</urn:desc2> 
    </urn:header>
    <urn:itens1>
        <urn:item1>undefined
            <urn:name1>undefined</urn:name1>
            <urn:name2>undefined</urn:name2>
        </urn:item1>
    </urn:itens1>
    <urn:itens2>
        <urn:item1>undefined
            <urn:name1>undefined</urn:name1>
            <urn:name2>undefined</urn:name2>
        </urn:item1>
        <urn:item2>undefined
            <urn:name1>undefined</urn:name1>
            <urn:name2>undefined</urn:name2>
        </urn:item2>
    </urn:itens2>
</urn:client>

XML output

<urn:client xmlns:urn='www.testing.com' xmlns:x='http://schemas.xmlsoap.org/soap/envelope/'>
    <header>
        <desc1>undefined</desc1>
        <desc2>undefined</desc2> 
    </header>
    <itens1>
        <item1>undefined
            <name1>undefined</name1>
            <name2>undefined</name2>
        </item1>
    </itens1>
    <itens2>
        <item1>undefined
            <name1>undefined</name1>
            <name2>undefined</name2>
        </item1>
        <item2>undefined
            <name1>undefined</name1>
            <name2>undefined</name2>
        </item2>
    </itens2>
</urn:client>

I tried with the following XSLT code

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <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:stylesheet>

However, the prefix and namespace from the root node also is being removed.

Dou you guys have any ideia?

Tks

Upvotes: 0

Views: 746

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

How about:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="/*">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>      
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

This assumes the attributes in the input XML are in no-namespace (as they usually will be). In fact, the given example has no attributes at all - so you could remove the <xsl:copy-of select="@*"/> instruction altogether.

Upvotes: 1

Related Questions