如何添加previous-sibling的属性,其中属性具有字母数字值

时间:2014-09-25 14:10:46

标签: xslt

建议如何添加previous-sibling的属性(字母数字文本)。在输入xml中,如果像“namest”这样的属性只有数字,那么添加属性就可以了。如果属性具有字母数字数据,则XSLT在'xsl:attribute name =“cellNum”'处获得错误。请建议。 (XSLT 2)。

输入XML:

<article>
<floats>
<table>
    <tbody>
        <tr><entry>1</entry><entry namest="col2" nameend="col5">2-5</entry><entry namest="col6" nameend="col9">6-9</entry><entry>10</entry><entry namest="col11" nameend="col13">11-13</entry><entry>14</entry></tr>
        <tr><entry>2</entry><entry namest="col2" nameend="col5">2-5</entry><entry namest="col6" nameend="col9">6-9</entry><entry>11</entry></tr>
    </tbody>
</table>
</floats>
</article>

XSLT:

<xsl:stylesheet version="2.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="tr">
        <tr>
        <xsl:for-each select="entry">
            <xsl:variable name="varNameST" select="sum(number(substring-after(preceding-sibling::entry/@namest, 'col')))"/>
            <xsl:variable name="varNameEND" select="sum(number(substring-after(preceding-sibling::entry/@nameend, 'col')))"/>
            <xsl:variable name="varCellcount"><xsl:number count="entry" format="1" level="single"/></xsl:variable>
            <xsl:variable name="varColspan"><xsl:value-of select="sum($varNameEND)-sum($varNameST)+sum($varCellcount)"/></xsl:variable>
            <entry>
                <xsl:attribute name="cellNum"><xsl:value-of select="$varColspan"/></xsl:attribute>
                <xsl:apply-templates/>
            </entry>
        </xsl:for-each>
        </tr>
    </xsl:template>

</xsl:stylesheet>

必需输出:

<article>
<floats>
<table>
    <tbody>
        <tr><entry cellNum="1">1</entry><entry cellNum="2">2-5</entry><entry cellNum="6">6-9</entry><entry cellNum="10">10</entry><entry cellNum="11">11-13</entry><entry cellNum="14">14</entry></tr>
        <tr><entry cellNum="1">1</entry><entry cellNum="2">2-5</entry><entry cellNum="6">6-9</entry><entry cellNum="10">10</entry><entry cellNum="11">11-13</entry><entry cellNum="14">14</entry></tr>
    </tbody>
</table>
</floats>

1 个答案:

答案 0 :(得分:1)

我想你想用

        <xsl:variable name="varNameST" select="sum(preceding-sibling::entry/@namest/number(substring-after(., 'col')))"/>

但是只能使用XSLT 2.0。