Gleb Belyaev
Gleb Belyaev

Reputation: 1049

How to remove the namespace if it is not used?

I have the xml:

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <med:PutEmployee xmlns:med="https://services">
      <med:employees>
         <med:Employee>
            <med:Name xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true">Мария</med:Name>
            <med:SNILS>111-111-111-11</med:SNILS>
         </med:Employee>
      </med:employees>
   </med:PutEmployee>
</soapenv:Body>

I deleted the parametr "@i:nill" using xslt:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
   exclude-result-prefixes="i">               
   <xsl:template match="node() | @*">
      <xsl:copy>
         <xsl:apply-templates select="node() | @*[name()!='i:nil']" />
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

running the xslt, I got the xml:

<?xml version="1.0"?>
<?xml version="1.0"?>
<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <med:PutEmployee xmlns:med="https://services">
      <med:employees>
         <med:Employee>
            <med:Name xmlns:i="http://www.w3.org/2001/XMLSchema-instance">Мария</med:Name>
            <med:SNILS>111-111-111-11</med:SNILS>
         </med:Employee>
      </med:employees>
   </med:PutEmploy>

left the xmlns:i="http://www.w3.org/2001/XMLSchema-instance"

how to remove it?

I tried to add exclude-result-prefixes = "i", it did not help

Upvotes: 0

Views: 190

Answers (2)

Michael Kay
Michael Kay

Reputation: 163645

If you are using XSLT 2.0, use

<xsl:copy copy-namespaces="no">

Upvotes: 3

JLRishe
JLRishe

Reputation: 101768

This should do the trick:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
                exclude-result-prefixes="i">
  <xsl:output omit-xml-declaration="yes"/>

  <xsl:template match="node() | @*" priority="-2">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@i:nil" />

  <xsl:template match="*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

When run on your sample input, the result is:

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <med:PutEmployee xmlns:med="https://services">
    <med:employees>
      <med:Employee>
        <med:Name>Мария</med:Name>
        <med:SNILS>111-111-111-11</med:SNILS>
      </med:Employee>
    </med:employees>
  </med:PutEmployee>
</soapenv:Body>

Upvotes: 2

Related Questions