我正在尝试转换此 xml:
<tokens>
<token cle="a">
<token cle="b">nomX</token>
<token cle="c">prenomX</token>
<token cle="d">villeX</token>
</token>
<token cle="a">
<token cle="b">nomY</token>
<token cle="c">prenomY</token>
<token cle="d">villeY</token>
</token>
<token cle="e">nomZ</token>
</tokens>
进入这个 xml:
<tokens>
<a>
<b>nomX</b>
<c>prenomX</c>
<d>villeX</d>
</a>
<a>
<b>nomY</b>
<c>prenomY</c>
<d>villeY</d>
</a>
<e>nomZ</e>
</tokens>
因此将属性值转换为元素,但我需要保留整个结构和深度。
我试过使用 XSL,但还没有成功。 如果有人有想法,将不胜感激。
谢谢。
最佳答案
所以 xslt 是我认为的正确方式:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="token">
<xsl:element name="{@cle}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
关于xml - 将属性值转换为元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4733880/