2012-07-12 55 views
2

我喜欢XMLXSL数连续节点

<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} 

我能做到这一点?

+0

你想用XSLT 2.0或1.0解决这个问题吗? 2.0有''这样应该很容易。 – 2012-07-12 13:33:41

+0

@MartinHonnen我使用xslt 1.0 – 2012-07-12 13:39:59

回答

2

该转化短期(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()],'}}','&#xA;')"/> 
    <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()],'}}','&#xA;')"/> 
</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} 
1

有很好的方式做到这一点,并有可恶的做法。我没有足够的时间去思考的一个很好的方法,它如何抢你?:

<?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 &gt; 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) &lt; 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() &gt; 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} 

我的假设是,如果你只有一个号码,你没我们不想继续下去,所以你不要继续,并且他们必须按顺序排列,并且必须在class ='x'中。

有了更多时间,你可以想出更漂亮的东西!

作为一个解释,它通过寻找一个td [@ class ='x']谁的数字不等于前一个+1(即没有数字或它不是连续的)。如果它找到一个,它检查下一个数字是这个数字+1,为了避免连续{1-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>}&#10;</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} 
1

我能想出的最简单的样式表解决您的问题:

<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>}&#10;</xsl:text> 
    </xsl:template> 
    <xsl:template match="td"/> 
</xsl:stylesheet> 

说明:首先,我们匹配每一个td其直接前置兄弟本身不是一个class一个td属性等于x。这些是开始节点,所以我们使用它们的值作为每个序列的开始。

对于我们使用的下一个td序列的末端,不具有作为其紧跟着兄弟另一个tdclassx

所有其他节点都被忽略。