我有这样一个 xml:
<person name="foo" gender = "male" />
我想把它改造成
<person id="foo" gender="male" />
有没有办法使用 XSLT 来做到这一点?
我会亲自出很多子节点
我会在这个人身上有更多的属性。
最佳答案
这非常简单:使用身份转换并创建一个转换 name
属性的模板:
<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,例如人/@name
。
关于xml - 如何使用 XSLT 重命名属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2679443/