如何使用 XSLT 重命名属性? [英] How do I rename an attribute using XSLT?
本文介绍了如何使用 XSLT 重命名属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的 xml:
I have an xml like this:
<person name="foo" gender = "male" />
我想把它改成
<person id="foo" gender="male" />
有没有办法使用 XSLT 做到这一点?
Is there a way to do that using XSLT?
我自己会有很多子节点
I will have a lot of child nodes in person
我会在这个人身上拥有更多的特质.
I will have more attributes in the person.
推荐答案
这很简单:使用身份转换并创建一个转换 name
属性的模板:
This is very simple: Use the identity transform and create a template that transforms the name
attribute:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@name">
<xsl:attribute name="id">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
这将使文档中除 name
属性之外的所有内容保持原样.如果您只想更改 person
元素上的 name
属性,请在模板的 match
属性中放置更严格的 XPath,例如person/@name
.
This will leave everything in the document except for name
attributes exactly as it is. If you only want to change the name
attribute on person
elements, put a more restrictive XPath in the template's match
attribute, e.g. person/@name
.
这篇关于如何使用 XSLT 重命名属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文