Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT - remove duplicate namespace declarations

Tags:

xml

xslt

I have the following xml:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig">
  <ds:SignedInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
  </ds:SignedInfo>
  <ds:SignedInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
     <ds:SignedInfoData xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
  </ds:SignedInfo/>
</ds:Signature>

The problem is, while I need the first ds namespace declaration in <ds:Signature>. the following ones (in <ds:SignedInfo> and <ds:SignedInfoData>) are not required. Is there any way to remove duplicate namespace declarations using XSLT 1.0 to get this output:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig">
  <ds:SignedInfo>
  </ds:SignedInfo>
  <ds:SignedInfo>
     <ds:SignedInfoData/>
  </ds:SignedInfo>
</ds:Signature>
like image 988
rfg Avatar asked Feb 15 '26 05:02

rfg


1 Answers

Eliminating the duplicated namespace declarations is something that happens by just copying the input, for instance with an identity transformation http://xsltransform.net/jxDigU1/1

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>
like image 72
Martin Honnen Avatar answered Feb 18 '26 05:02

Martin Honnen