gpt4 book ai didi

xslt - XSL - 复制元素但删除未使用的命名空间

转载 作者:行者123 更新时间:2023-12-03 13:31:33 24 4
gpt4 key购买 nike

我有一些 XML 声明了一个仅用于属性的命名空间,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:x="http://tempuri.com">
<b>
<c x:att="true"/>
<d>hello</d>
</b>
</a>

我想使用 XSL 创建所选节点及其值的副本 - 摆脱属性。所以我想要的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<b>
<c />
<d>hello</d>
</b>

我有一些 XSL 几乎可以做到这一点,但我似乎无法阻止它将命名空间声明放在输出的顶级元素中。我的 XSL 是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates select="/a/b"/>
</xsl:template>

<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

输出的第一个元素是 <b xmlns:x="http://tempuri.com">而不是 <b> .我尝试在 XSL 中声明命名空间并将前缀放在 exclude-result-prefixes 中列表,但这似乎没有任何效果。我究竟做错了什么?

更新:我发现通过在 XSL 中声明命名空间并使用 extension-element-prefixes属性有效,但这似乎不对!我想我可以使用它,但我想知道为什么 exclude-result-prefixes不工作!

更新:实际上,这似乎是 extension-element-prefixes解决方案仅适用于 XMLSpy 的内置 XSLT 引擎,不适用于 MSXML。

最佳答案

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:x="http://tempuri.com">
<xsl:template match="/">
<xsl:apply-templates select="/a/b"/>
</xsl:template>

<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:copy/>
</xsl:template>

<!-- This empty template is not needed.
Neither is the xmlns declaration above:
<xsl:template match="@x:*"/> -->
</xsl:stylesheet>

我找到了解释 here .

Michael Kay wrote:
exclude-result-prefixes only affects the namespaces copied from the stylesheet by a literal result element, it doesn't affect copying of namespaces from source documents.

关于xslt - XSL - 复制元素但删除未使用的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1074767/

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