我有这个示例文档:
<root>
<span class="a">te</span><span class="a">st</span> <span class="a">tag</span>
some other text
<span class="b">keep</span>
</root>
我想有这样的事情:
<root>
<span class="a">test tag</span>
some other text
<span class="b">keep</span>
</root>
或者至少这个:
<root>
<span class="a">test</span> <span class="a">tag</span>
some other text
<span class="b">keep</span>
</root>
class="a"
跨度可变计数。
在XSLT中可以吗?
修改
试图解决:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/span[@class = 'a']"/>
<xsl:template match="*/span[@class = 'a'][1]">
<span class="a">
<xsl:for-each select="../span[@class = 'a'] | ../text()[. = ' ']">
<xsl:choose>
<xsl:when test=". = ' '">
<xsl:copy/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="./text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</span>
</xsl:template>
</xsl:stylesheet>
但是当我有这个输入时?
<root>
<span class="a">te</span><span class="a">st</span> <span class="a">tag</span>
some other text
<span class="b">keep</span>
<span class="a">te</span><span class="a">st</span> <span class="a">tag</span>
</root>
如何解决这个问题?
答案 0 :(得分:0)
第二种变体可以通过以下方式实现:
XSLT 1.0
<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:key name="grp"
match="span[@class='a'][preceding-sibling::node()[1][self::span[@class='a']]]"
use="generate-id(preceding-sibling::span[@class='a'][not(preceding-sibling::node()[1][self::span[@class='a']])][1])" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="span[@class='a'][not(preceding-sibling::node()[1][self::span[@class='a']])]" priority="1">
<span class="a">
<xsl:for-each select=". | key('grp', generate-id())" >
<xsl:value-of select="."/>
</xsl:for-each>
</span>
</xsl:template>
<xsl:template match="span[@class='a']"/>
</xsl:stylesheet>
通过更多工作,您可以在组中包含散布的仅限空格的文本节点。或者,看一下称为“兄弟递归”的技术。