gpt4 book ai didi

.net - 从 XSLT 输出根中删除 XML 命名空间声明?

转载 作者:数据小太阳 更新时间:2023-10-29 01:49:27 25 4
gpt4 key购买 nike

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

如何从根元素中删除 namespace 声明?

最佳答案

在 XSLT 2.0 中你可以使用

<xsl:copy copy-namespaces="no">

关于.net - 从 XSLT 输出根中删除 XML 命名空间声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com