XSLT - 识别节点后跟另一个节点
XSLT - identify node followed by another node
我有一个这样的xml,
<doc>
<p>para<x>para</x>para<x>para</x>para</p>
<p>para<x>para</x><x>para</x>para</p>
</doc>
如果连续放置几个 <x>
(<x>
后跟另一个 <x>
节点),我需要在 <x>
节点之间添加一个 ','。
所以,对于上面的例子 xml,输出应该是,
<doc>
<p>para<x>para</x>para<x>para</x>para</p>
<p>para<x>para</x>,<x>para</x>para</p>
</doc>
我尝试编写一个 xsl 模板来识别连续 <x>
注释并添加了 ',' 如下,
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="x[following-sibling::*[1][self::x]]">
<x>
<xsl:apply-templates/>
</x>
<xsl:text>,</xsl:text>
</xsl:template>
但它在上述两种情况下都添加了“,”。 (<x>
后跟另一个 <x>
节点和 <x>
后跟文本)
有没有更正此 xpath 的想法?
通过使用 following-sibling::*[1]
,XPath 仅检查最近的后续同级 元素 ,而不考虑 文本节点 。尝试使用 following-sibling::node()[1]
代替:
<xsl:template match="x[following-sibling::node()[1][self::x]]">
<x>
<xsl:apply-templates/>
</x>
<xsl:text>,</xsl:text>
</xsl:template>
我有一个这样的xml,
<doc>
<p>para<x>para</x>para<x>para</x>para</p>
<p>para<x>para</x><x>para</x>para</p>
</doc>
如果连续放置几个 <x>
(<x>
后跟另一个 <x>
节点),我需要在 <x>
节点之间添加一个 ','。
所以,对于上面的例子 xml,输出应该是,
<doc>
<p>para<x>para</x>para<x>para</x>para</p>
<p>para<x>para</x>,<x>para</x>para</p>
</doc>
我尝试编写一个 xsl 模板来识别连续 <x>
注释并添加了 ',' 如下,
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="x[following-sibling::*[1][self::x]]">
<x>
<xsl:apply-templates/>
</x>
<xsl:text>,</xsl:text>
</xsl:template>
但它在上述两种情况下都添加了“,”。 (<x>
后跟另一个 <x>
节点和 <x>
后跟文本)
有没有更正此 xpath 的想法?
通过使用 following-sibling::*[1]
,XPath 仅检查最近的后续同级 元素 ,而不考虑 文本节点 。尝试使用 following-sibling::node()[1]
代替:
<xsl:template match="x[following-sibling::node()[1][self::x]]">
<x>
<xsl:apply-templates/>
</x>
<xsl:text>,</xsl:text>
</xsl:template>