Rajendra Koduru
Rajendra Koduru

Reputation: 3

XSLT Remove XMLNS (Name Space) Attribute matching a specific value

I would like to remove a namespace with a specific value from all the elements that have it. I am looking for XSLT transformation for it

As shown below , i need to remove xmlns="NS_647" from the elements that contain it

Input XML

 <COSR xmlns="TEST_NS1" >
    <ORM_O01.PATIENT xmlns="NS_647" >
      <PID>
       <PID.1>1</PID.1>
       </PID>
     </ORM_O01.PATIENT>  
  </COSR>

Output

<COSR xmlns="TEST_NS1" >
    <ORM_O01.PATIENT >
      <PID>
       <PID.1>1</PID.1>
       </PID>
     </ORM_O01.PATIENT>  
 </COSR>

Upvotes: 0

Views: 1201

Answers (2)

Michael Kay
Michael Kay

Reputation: 163458

XSLT operates on a tree of nodes described by the XDM data model, and not on raw lexical XML. In the XDM tree representation of your input, the xmlns="NS_647" is not present as an attribute node. Instead, the namespace declaration has two effects:

  1. it changes the namespace URI part of the element names within its scope, and
  2. it causes all elements within its scope to have a namespace node reflecting the binding of the empty prefix to the URI "NS_647" (incidentally, relative URIs as namespace names are deprecated, but we'll let that pass).

So what you actually want to do in your transformation is to change the relevant elements to be in namespace "TEST_NS1" rather than in "NS_647". You can achieve that with the template rule:

<xsl:template match="*[namespace-uri()='NS_647']">
  <xsl:element name="{local-name(.)}" namespace="TEST_NS1">
    <xsl:apply-templates select="@* | node()"/>
  </xsl:element>
</xsl:template>

Upvotes: 4

Damian
Damian

Reputation: 2852

Try this xslt

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

Upvotes: 0

Related Questions