Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove XML namespace declaration from XSLT output root?

Having the following input XML:

<?xml version="1.0" encoding="utf-8" ?>
<customSettings xmlns:env="urn:schemas-test-env">
    <connectionStrings>
        <add name="Name" connectionString="None" providerName="" />
        <add name="Name" connectionString="Local"  providerName="" env:name="Local" />
        <add name="Name" connectionString="Dev"  providerName="" env:name="Dev"  />
    </connectionStrings>
    <appSettings>
        <add key="Name" value="Value" />
        <add key="Name" value="Local" env:name="Local" />
        <add key="Name" value="Dev" env:name="Dev" />
    </appSettings>
</customSettings>

and XSLT:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"

                xmlns:code="urn:schemas-test-code"
                xmlns:env="urn:schemas-test-env"
>
    <xsl:output version="1.0" encoding="utf-8" omit-xml-declaration="yes" indent="yes" />
    <xsl:strip-space elements="*" />

    <!-- Populate param value -->
    <xsl:param name="env" select="code:GetEnvironment()" />

    <!-- Copy content as is -->
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <!-- Remove all add nodes with env:name not matching param -->
    <xsl:template match="add">
        <xsl:if test="not(@env:name != $env)">
            <xsl:call-template name="identity" />
        </xsl:if>
    </xsl:template>

    <!-- Remove all env:name attributes -->
    <xsl:template match="@env:name" />
</xsl:stylesheet>

I'm getting the following output XML:

<customSettings xmlns:env="urn:schemas-test-env">
  <connectionStrings>
    <add name="Name" connectionString="None" providerName="" />
    <add name="Name" connectionString="Local" providerName="" />
  </connectionStrings>
  <appSettings>
    <add key="Name" value="Value" />
    <add key="Name" value="Local" />
  </appSettings>
</customSettings>

How to remove namespace declaration from the root element?

like image 316
abatishchev Avatar asked Mar 10 '13 04:03

abatishchev


2 Answers

In XSLT 2.0 you can use

<xsl:copy copy-namespaces="no">
like image 131
Michael Kay Avatar answered Nov 15 '22 11:11

Michael Kay


If you replace this template:

<!-- Copy content as is -->
<xsl:template match="node()|@*" name="identity">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

With these two templates:

<!-- Copy elements without copying their namespace declarations -->
<xsl:template match="*" name="identity">
  <xsl:element name="{name()}">
    <xsl:apply-templates select="node()|@*" />
  </xsl:element>
</xsl:template>

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

Then that should do it.

like image 35
JLRishe Avatar answered Nov 15 '22 09:11

JLRishe