6

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?

3 Answers 3

5

In XSLT 2.0 you can use

<xsl:copy copy-namespaces="no">
Sign up to request clarification or add additional context in comments.

3 Comments

I use .NET so unfortunately limited with 1.0.
Not so, .NET is well served with XSLT 2.0 processors - there are two excellent ones, Saxon and XmlPrime.
Yes, sure, there are a lot but not out of the box. That's what I mean
4

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.

6 Comments

Is it possible do not use priority?
@abatischev no, because match="*" and match="node()" are the same priority by default.
@abatishchev I think you don't need the priority attribute if you simply use <xsl:template match="text()|@*"><xsl:copy/></xsl:template> instead of the second template.
@nwellnhof it would have to be match="text()|comment()|processing-instruction()|@*"
@abatishchev Is there a reason you want to avoid using priority? An identity template should typically have lower priority than anything else in the XSLT, so making its priority extra low should probably not cause any issues.
|
3

Have you tried adding exclude-result-prefixes="env" to your stylesheet declaration? Should work, like this:

<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"
                exclude-result-prefixes="env"
>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.