加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 08:02:01 所属栏目:百科 来源:网络整理
导读:我有一些XML声明一个仅用于属性的命名空间,如下所示: ?xml version="1.0" encoding="UTF-8"?a xmlns:x="http://tempuri.com" b c x:att="true"/ dhello/d /b/a 我想使用XSL来创建所选节点及其值的副本 – 摆脱属性。所以我想要的输出是: ?xml version="1.
我有一些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属性可以工作,但是这似乎不正确!我想我可以使用这个,但我想知道为什么排除结果前缀不起作用!

更新:实际上,这个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读