XSL - 复制元素但删除未使用的命名空间 [英] XSL - copy elements but remove unused namespace(s)

查看:27
本文介绍了XSL - 复制元素但删除未使用的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got some XML which declares a namespace which is only used for attributes, like this:

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

我想使用 XSL 创建所选节点及其值的副本 - 删除属性.所以我想要的输出是:

I want to use XSL to create a copy of selected nodes and their values - getting rid of the attributes. So my desired output is:

<?xml version="1.0" encoding="UTF-8"?>
<b>
    <c />
    <d>hello</d>
</b>

我有一些几乎可以做到这一点的 XSL,但我似乎无法阻止它将命名空间声明放在输出的顶级元素中.我的 XSL 是:

I've got some XSL that almost does this, but I can't seem to stop it putting the namespace declaration in the top level element of the output. My XSL is:

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

输出的第一个元素是 而不是 .我已经尝试在 XSL 中声明命名空间并将前缀放在 exclude-result-prefixes 列表中,但这似乎没有任何效果.我做错了什么?

The first element of the output is <b xmlns:x="http://tempuri.com"> instead of <b>. I've tried declaring the namespace in the XSL and putting the prefix in the exclude-result-prefixes list, but this doesn't seem to have any effect. What am I doing wrong?

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

UPDATE: I've found that by declaring the namespace in the XSL and using the extension-element-prefixes attribute works, but this doesn't seem right! I guess I could use this, but I'd like to know why the exclude-result-prefixes doesn't work!

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

UPDATE: Actually, it seems this extension-element-prefixes solution only works with XMLSpy's built-in XSLT engine, not with 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>

我在此处找到了说明.

迈克尔·凯写道:
exclude-result-prefixes 只影响从文字结果元素的样式表,它不影响复制来自源文档的命名空间.

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.

这篇关于XSL - 复制元素但删除未使用的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆