我有xml之类的
<tr>
<td class="x">1</td>
<td class="x">2</td>
<td>3</td>
<td class="x">4</td>
<td class="x">5</td>
<td class="x">6</td>
<td class="x">7</td>
</tr>
我希望使用 xsl 的结果是:
\cont{1-2}
\cont{4-7}
我可以这样做吗?
我有xml之类的
<tr>
<td class="x">1</td>
<td class="x">2</td>
<td>3</td>
<td class="x">4</td>
<td class="x">5</td>
<td class="x">6</td>
<td class="x">7</td>
</tr>
我希望使用 xsl 的结果是:
\cont{1-2}
\cont{4-7}
我可以这样做吗?
这种转换既短(25 行)又高效(使用键):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kFollowing" match="td[@class='x']"
use="concat(generate-id(..),
'+',generate-id(preceding-sibling::*
[not(self::td and @class='x')][1])
)"/>
<xsl:template match="/*">
<xsl:variable name="vGroup"
select="key('kFollowing', concat(generate-id(),'+'))"/>
<xsl:value-of select=
"concat('\cont{{',$vGroup[1],'-',$vGroup[last()],'}}','
')"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*/*">
<xsl:variable name="vGroup" select=
"key('kFollowing', concat(generate-id(..),'+', generate-id()))"/>
<xsl:value-of select=
"concat('\cont{{',$vGroup[1],'-',$vGroup[last()],'}}','
')"/>
</xsl:template>
<xsl:template match="td[@class='x']|text()"/>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<tr>
<td class="x">1</td>
<td class="x">2</td>
<td>3</td>
<td class="x">4</td>
<td class="x">5</td>
<td class="x">6</td>
<td class="x">7</td>
</tr>
产生了想要的正确结果:
\cont{1-2}
\cont{4-7}
有很好的方法,也有讨厌的方法。我没有足够的时间想一个好的方法,这如何抓住你?:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common">
<xsl:output method="text" indent="no"/>
<xsl:template match="/tr">
<xsl:for-each select="td[@class='x']">
<xsl:variable name="currentNum" select="number(text())"/>
<xsl:variable name="prevNum" select="number(preceding-sibling::td[@class='x'][1]/text())"/>
<xsl:if test="($currentNum - 1) != $prevNum and $currentNum > 0 and following-sibling::td[@class='x']">
<xsl:variable name="following" select="following-sibling::td[@class='x']"/>
<xsl:if test="number($following[1]/text()) = ($currentNum + 1)">
\cont{<xsl:value-of select="$currentNum"/>-<xsl:call-template name="findend">
<xsl:with-param name="start" select="$currentNum"/>
<xsl:with-param name="nodes" select="$following"/>
</xsl:call-template>}
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="findend">
<xsl:param name="nodes"/>
<xsl:param name="start"/>
<xsl:variable name="current" select="number($nodes[1]/text())"/>
<xsl:choose>
<xsl:when test="count($nodes) < 2">
<xsl:value-of select="$current"/>
</xsl:when>
<xsl:when test="number($nodes[2]/text()) != ($current + 1)">
<xsl:value-of select="$current"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="findend">
<xsl:with-param name="nodes" select="$nodes[position() > 1]"/>
<xsl:with-param name="start" select="$current"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
输出是
\cont{1-2}
\cont{4-7}
我做了一个假设,如果你只有一个数字,你不想继续,所以你不会得到 \cont{1-1},而且它们必须按顺序排列,并且它们必须在 class= 'X'。
多花点时间,你可以想出更漂亮的东西!
不过,作为解释,它会通过寻找一个 td[@class='x'] 的数字不等于前一个 + 1 (即,没有数字或它不连续)。如果找到一个,它会检查下一个数字是这个数字 + 1,以避免出现 cont{1-1} 的情况,然后它调用一个递归模板来查找并打印结尾
这是一些使用键和模式的 XSLT 1.0 方法(但主要是为了避免复杂的模板匹配模式):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="k1"
match="td[@class = 'x'][preceding-sibling::*[1][self::td[@class = 'x']]]"
use="generate-id(
preceding-sibling::td[
@class = 'x' and
not(preceding-sibling::*[1][self::td[@class = 'x']])
][1]
)"/>
<xsl:template match="tr">
<xsl:apply-templates select="td[@class = 'x' and not(preceding-sibling::*[1][self::td[@class = 'x']])]" mode="start"/>
</xsl:template>
<xsl:template match="td[@class = 'x']" mode="start">
<xsl:text>\cont{</xsl:text>
<xsl:value-of select="."/>
<xsl:text>-</xsl:text>
<xsl:apply-templates select="key('k1', generate-id())[last()]" mode="end"/>
</xsl:template>
<xsl:template match="td[@class = 'x']" mode="end">
<xsl:value-of select="."/>
<xsl:text>} </xsl:text>
</xsl:template>
</xsl:stylesheet>
该样式表,当应用于
<tr>
<td class="x">1</td>
<td class="x">2</td>
<td>3</td>
<td class="x">4</td>
<td class="x">5</td>
<td class="x">6</td>
<td class="x">7</td>
</tr>
输出
\cont{1-2}
\cont{4-7}
我能想出的最简单的样式表可以解决您的问题:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="td[not(preceding-sibling::*[1][self::td][@class='x'])]">
<xsl:text>\cont{</xsl:text>
<xsl:value-of select="."/>
<xsl:text>-</xsl:text>
<xsl:value-of select="following-sibling::td[
not(following-sibling::*[1][self::td][@class='x'])][1]"/>
<xsl:text>} </xsl:text>
</xsl:template>
<xsl:template match="td"/>
</xsl:stylesheet>
解释: 首先,我们匹配td
其前一个兄弟本身不是 a 的每个td
具有class
等于 的属性x
。这些是开始节点,所以我们使用它们的值作为每个序列的开始。
对于序列的结尾,我们使用下一个td
不具有的下一个作为其紧随其后的兄弟td
,另一个class
是x
。
所有其他节点都被忽略。