XSLT:如何在期间更改属性值?
我有一个 XML 文档,我想更改其中一个属性的值。
首先,我使用以下方法复制了从输入到输出的所有内容:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
现在我想更改任何名为 "property"
的元素中的属性 "type"
的值。
I have an XML document, and I want to change the values for one of the attributes.
First I copied everything from input to output using:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
And now I want to change the value of the attribute "type"
in any element named "property"
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这个问题有一个经典的解决方案:使用并覆盖 身份模板是最基本、最强大的 XSLT 设计模式之一:
应用于此 XML 文档时:
想要的产生结果:
This problem has a classical solution: Using and overriding the identity template is one of the most fundamental and powerful XSLT design patterns:
When applied on this XML document:
the wanted result is produced:
在一个简单的示例上进行了测试,工作正常:
编辑以包含托马拉克的建议。
Tested on a simple example, works fine:
Edited to include Tomalak's suggestion.
如果根元素中有 xmlns 定义,那么前两个答案将不起作用:
所有解决方案都不适用于上述 xml。
可能的解决方案是这样的:
The top two answers will not work if there is a xmlns definition in the root element:
All of the solutions will not work for the above xml.
The possible solution is like:
您需要一个与您的目标属性相匹配的模板,仅此而已。
这是对您已经拥有的“全部复制”的补充(并且实际上默认情况下始终存在于 XSLT 中)。 如果有更具体的匹配,将优先使用它。
You need a template that will match your target attribute, and nothing else.
This is in addition to the "copy all" you already have (and is actually always present by default in XSLT). Having a more specific match it will be used in preference.
我有一个类似的情况,我想从一个简单的节点中删除一个属性,但无法弄清楚哪个轴可以让我读取属性名称。 最后,我所要做的就是使用
@*[name(.)!='AttributeNameToDelete']
I had a similar case where I wanted to delete one attribute from a simple node, and couldn't figure out what axis would let me read the attribute name. In the end, all I had to do was use
@*[name(.)!='AttributeNameToDelete']
我也遇到了同样的问题,我解决了如下:
I also came across same issue and i solved it as follows:
对于以下 XML:
我能够使其与以下 XSLT 一起使用:
For the following XML:
I was able to get it to work with the following XSLT:
如果您的源 XML 文档有自己的命名空间,则需要在样式表中声明该命名空间,为其分配一个前缀,并在引用源 XML 的元素时使用该前缀 - 例如:
或者,如果您愿意的话:
ADDENDUM:
在极不可能的情况下,即事先不知道 XML 命名空间,您可以执行以下操作:
当然,很难想象这样一个场景:您提前知道源 XML 文档包含一个名为“property”的元素,以及一个需要替换的名为“type”的属性,但仍然不知道文档的名称空间。 我添加此内容主要是为了展示如何简化您自己的解决方案。
If your source XML document has its own namespace, you need to declare the namespace in your stylesheet, assign it a prefix, and use that prefix when referring to the elements of the source XML - for example:
Or, if you prefer:
ADDENDUM:
In the highly unlikely case where the XML namespace is not known beforehand, you could do:
Of course, it's very difficult to imagine a scenario where you would know in advance that the source XML document contains an element named "property", with an attribute named "type" that needs replacing - but still not know the namespace of the document. I have added this mainly to show how your own solution could be streamlined.