我正在尝试转换这个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,但还没有成功。如果任何人有一个想法,将不胜感激。
谢谢。
发布于 2011-01-19 18:24:10
所以我认为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>发布于 2011-01-19 18:18:20
这应该能起到作用:
<xsl:template match="token">
<xsl:element name="{@cle}">
<xsl:apply-templates select="*|@*"/>
</xsl:element>
</xsl:template>有关xsl:element的更多信息,请参阅:http://www.w3.org/TR/xslt#section-Creating-Elements-with-xsl:element
您可能希望添加一些xsl:if来检查是否真的有@cle属性,但除此之外,这应该可以很好地工作
发布于 2011-01-19 18:28:39
我使用您的答案找到了正确的xsl:
下面是我使用的代码:
<xsl:template match="token">
<xsl:element name="{@cle}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>非常感谢!
https://stackoverflow.com/questions/4733880
复制相似问题