Kaushik
Kaushik

Reputation: 165

Remove namespace from a tag

I want to print certain part of xml in the ouput. But while doing this , namespace is getting appended in the output of the tag. I wish to remove this namespace from output. Please help.

xmltoxslt.xsl file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match = "data">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

input.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:LogDel xmlns:ns0="http://world.com/pi/M/LogDeli">
   <data>
      <ERP>123</ERP>
      <Issuedate>2019-02-18</Issuedate>
      <Shippingpoint>2234</Shippingpoint>
      <Shiptoparty>XXXXXXX</Shiptoparty>
      <products>
         <SKUno>dfsf</SKUno>
         <QTY>50</QTY>
         <Unit>L</Unit>
      </products>
      <products>
         <SKUno>sfsdf</SKUno>
         <QTY>30532</QTY>
         <Unit>KG</Unit>
      </products>
   </data>
   <data>
      <ERP>345</ERP>
      <Issuedate>2019-03-07</Issuedate>
      <Shippingpoint/>
      <Shiptoparty>XXXXXXX</Shiptoparty>
      <products>
         <SKUno>dsfsf</SKUno>
         <QTY>1000</QTY>
         <Unit>L</Unit>
      </products>
      <products>
         <SKUno>sfsdf</SKUno>
         <QTY>300</QTY>
         <Unit>KG</Unit>
      </products>
   </data>
</ns0:LogDeli>

InputOuput.xml file:

<data xmlns:ns0="http://world.com/pi/M/LogDeli">
     <ERP>123</ERP>
      <Issuedate>2019-02-18</Issuedate>
      <Shippingpoint>2234</Shippingpoint>
      <Shiptoparty>XXXXXXX</Shiptoparty>
      <products>
         <SKUno>dfsf</SKUno>
         <QTY>50</QTY>
         <Unit>L</Unit>
      </products>
      <products>
         <SKUno>sfsdf</SKUno>
         <QTY>30532</QTY>
         <Unit>KG</Unit>
      </products>
   </data>
   <data xmlns:ns0="http://world.com/pi/M/LogDeli">
     <ERP>345</ERP>
      <Issuedate>2019-03-07</Issuedate>
      <Shippingpoint/>
      <Shiptoparty>XXXXXXX</Shiptoparty>
      <products>
         <SKUno>dsfsf</SKUno>
         <QTY>1000</QTY>
         <Unit>L</Unit>
      </products>
      <products>
         <SKUno>sfsdf</SKUno>
         <QTY>300</QTY>
         <Unit>KG</Unit>
      </products>
   </data>

I want to remove the namespace xmlns:ns0="http://world.com/pi/M/LogDeli" from the data tag. It should be like <data>

Upvotes: 1

Views: 473

Answers (2)

Tim C
Tim C

Reputation: 70618

Although xmlns:ns0="http://world.com/pi/M/LogDeli" is present on only the root element in the XML when viewed as text, when this lexical XML is parsed, all descendant elements will also have that declaration stored against them. This means, when you do xsl:copy-of then the declarations will also be copied.

What you need to do, is created new elements for data and all its descendants.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes"/>

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

On the other hand, if you can use XSLT 2.0, you can make use of copy-namespaces on the xsl:copy-of

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes"/>

  <xsl:template match="data">
    <xsl:copy-of select="." copy-namespaces="no" />
  </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Ajeet Singh
Ajeet Singh

Reputation: 1076

only following change:-

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://world.com/pi/M/LogDeli" exclude-result-prefixes="ns0">
    <xsl:output omit-xml-declaration="yes"/>

<xsl:template match = "data">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

Upvotes: 0

Related Questions