xsl:转换命名空间标签 [英] xsl: transform namespaced tags
问题描述
我有一个复制 xml 文件并重命名根标记的 xsl.
I have an xsl that copies a xml file and renames the root tag.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:abc="http://example.com">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<test>
<xsl:apply-templates select="node()|@*"/>
</test>
</xsl:template>
<!--xsl:template match="abc:set">
-<xsl:apply-templates select="node()|@*"/>-
</xsl:template-->
</xsl:stylesheet>
这很好用,但是当我取消注释最后一个块来处理一些命名空间标签时,我收到一个错误,说 copy 语句有问题.如何匹配和转换命名空间标签?
That works fine but when I uncomment the last block to handle some namespaced tags I got an error that the says that something is wrong with the copy statement. How can I match and transform namespaced tags?
推荐答案
您可能会收到错误消息,因为 abc:set
元素具有属性,并且您的模板与 匹配abc:set
正在生成未附加到元素的裸"属性.
You are likely getting an error because the abc:set
element has attributes, and your template matching on abc:set
is producing "naked" attributes that are not attached to an element.
由于您没有在 abc:set
的模板匹配中复制 abc:set
元素(或创建元素),当 apply-templates
在该模板内应用模板到选定的 abc:set/@*
和 abc:set/node()
,然后属性匹配身份模板并将被向前复制.
Since you are not copying the abc:set
element (or creating an element) in the template match for abc:set
, when the apply-templates
inside of that template applies the templates to the selected abc:set/@*
and abc:set/node()
, then the attributes match the identity template and will be copied forward.
您可以通过从 apply-templates
的 select 语句中取出 @*
来验证这是否是问题,如下所示:
You can verify whether that is the issue by taking the @*
out of the select statement for the apply-templates
, like this:
<xsl:template match="abc:set">
<xsl:apply-templates select="node()"/>
</xsl:template>
上面的模板只会处理abc:set
的子节点.
The template above will only process the child nodes of abc:set
.
如果您的意图是简单地复制 abc:set
,那么您不需要与该元素匹配的特定模板.身份模板将与之匹配并为您处理.
If your intent was to simply copy the abc:set
, then you don't need a specific template matching on that element. The identity template will match on it and handle that for you.
这篇关于xsl:转换命名空间标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!