我有一个这样的xml:
@H_404_1@<person name="foo" gender = "male" />
我想把它变成
@H_404_1@<person id="foo" gender="male" />有没有办法使用XSLT?
>我将有很多子节点
我会在这个人身上有更多的属性.
这很简单:使用身份转换并创建一个转换name属性的模板:
@H_404_1@<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>
这将保留文档中的所有内容,除了名称属性完全一样.如果您只想更改person元素上的name属性,则在模板的匹配属性中放置更多限制性XPath,例如.人/ @名称.