6

具有以下输入 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>

和 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>

我得到以下输出 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>

如何从根元素中删除命名空间声明?

4

3 回答 3

5

在 XSLT 2.0 中,您可以使用

<xsl:copy copy-namespaces="no">
于 2013-03-10T10:34:44.130 回答
4

如果您替换此模板:

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

使用这两个模板:

<!-- 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>

那么应该这样做。

于 2013-03-10T04:54:36.330 回答
3

您是否尝试将 exclude-result-prefixes="env" 添加到样式表声明中?应该工作,像这样:

<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"
>
于 2014-01-21T12:51:25.773 回答