我有一个像这样的xml:
<person name="foo" gender = "male" />
我想把它改造成
<person id="foo" gender="male" />
有没有办法使用XSLT做到这一点?
我会亲自拥有很多子节点
我会在这个人身上拥有更多属性。
我有一个像这样的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
属性完全一样。如果你只想改变 name
属性 person
元素,在模板中添加更严格的XPath match
属性,例如 person/@name
。
这应该这样做,不完全确定{name()}但你可以用“person”替换它
> <xsl:template match="person">
> <xsl:element name="{name()}">
> <xsl:attribute name="id">
> <xsl:value-of select="@name"/>
> </xsl:attribute>
> <xsl:attribute name="gender">
> <xsl:value-of select="@gender"/>
> </xsl:attribute>
> </xsl:element>
> </xsl:template>