我有以下源xml:
<to id="abc">
<ti></ti>
<b>
...
<to id="bcd"><ti></ti><b>...</b></to>
<to id="cde"><ti></ti><b>...</b></to>
<to id="def"><ti></ti><b>...</b></to>
</b>
</to>"...“意味着大量的bodydiv li和nodetext介于两者之间。
我想将其转换为:
<to id="abc">
<ti></ti>
<b>
...
</b>
<to id="bcd"><ti></ti><b>...</b></to>
<to id="cde"><ti></ti><b>...</b></to>
<to id="def"><ti></ti><b>...</b></to>
</to>在xslt中表达转换最简单的方法是什么?
发布于 2013-06-29 00:28:56
看起来您只是将to移到了b之外。我不确定为什么你需要基于@id。
试着这样做:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::to)]"/>
</xsl:copy>
<xsl:apply-templates select="to"/>
</xsl:template>
</xsl:stylesheet>发布于 2013-06-28 18:01:53
下面应该可以,它使用identity转换模板复制所有内容,并添加两个模板,第一个处理to[@id = 'abc']元素,第二个处理其b子元素:
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="to[@id = 'abc']">
<xsl:copy>
<xsl:apply-templates select="@* | node() | b/to[@id]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="to[@id = 'abc']/b">
<xsl:copy>
<xsl:apply-templates select="@* | node()[not(self::to[@id])]"/>
</xsl:copy>
</xsl:template>https://stackoverflow.com/questions/17360406
复制相似问题