将节点属性复制到 xslt 中的新节点 [英] Copying node attribute to a new node in xslt

查看:30
本文介绍了将节点属性复制到 xslt 中的新节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的xml:

<definitions xmlns:xxx="test.com" xmlns:yyy="test2.com" test="test">
</definitions>

我需要像这样转换:

<test xmlns:xxx="test.com" xmlns:yyy="test2.com" test="test">
</test>

我写了一个这样的 xslt:

I wrote an xslt like this:

<xsl:template match="definitions">
    <xsl:element name="test">
        <xsl:copy-of select="@*" />
    </xsl:element>
</xsl:template>

这会产生:

<test test="test">
</test>

但它不包含 xmlns:xxx="test.com" xmlns:yyy="test2.com" 命名空间为什么?

but it doesnt contain xmlns:xxx="test.com" xmlns:yyy="test2.com" namespaces why?

如何与命名空间一起复制?

How can I copy along with namespaces also?

推荐答案

为什么不包含 xmlns:xxx="test.com" xmlns:yyy="test2.com" 命名空间?

it doesnt contain xmlns:xxx="test.com" xmlns:yyy="test2.com" namespaces why?

它不包含命名空间声明,因为它们不在任何地方使用 - 所以 XSLT 处理器不输出它们.

It doesn't contain the namespace declarations because they are not used anywhere - so the XSLT processor does not output them.

如何与命名空间一起复制?

How can I copy along with namespaces also?

我不明白你为什么想要它们 - 但如果你坚持,你可以明确地复制它们:

I don't see why you would want them - but if you insist, you could copy them explicitly:

<xsl:template match="definitions">
    <test>
        <xsl:copy-of select="@* | namespace::*" />
    </test>
</xsl:template>

请注意,当元素名称已知时,不必使用 xsl:element.

Note that that it's not necessary to use xsl:element when the name of the element is known.

这篇关于将节点属性复制到 xslt 中的新节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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