具有以下输入
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>
如何从根元素中删除命名空间声明?
如果您替换此模板:
原文链接:/xml/292298.html<!-- 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>
那应该这样做.