开发者

Accumulation Variable in XSLT

开发者 https://www.devze.com 2023-03-30 01:47 出处:网络
I have a table, each row consists of 7 columns, and in the form of 5 cells: <ROW> <CELL ROWSPAN=\"1\" COLSPAN=\"1\">1</CELL>

I have a table, each row consists of 7 columns, and in the form of 5 cells:

 <ROW>
   <CELL ROWSPAN="1" COLSPAN="1">1</CELL>
   <CELL ROWSPAN="1" COLSPAN="1">Mandrel</CELL>
   <CELL ROWSPAN="1" COLSPAN="1">1</CELL>
   <CELL ROWSPAN="1" COLSPAN="2">H079249303</CELL>
   <CELL ROWSPAN="1" COLSPAN="2">H079249301</CELL>
 </ROW>

Now my template match against each individual <CELL> and turn them into <entry>, and for those with a @COLSPAN value greater than 1 (like the last two cells above), I need to have a namest and namend attribute for them, so for example, the above code will turn into:

  <row>
    <entry>1</entry>
    <entry>Mandrel</entry>
    <entry>1</entry>
    <entry namest="c4" nameend="c5">H079249303</entry>
    <entry namest="c6" nameend="c7">H079249301</entry>
  </row>

I am able 开发者_开发百科to keep track of the number of preceding siblings, but really have no idea of how to sum the @COLSPAN attribute of all preceding siblings since XSLT doesn't allow variable increment.

Thanks for help.


Look at sum function, i.e.:

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="ROW">
    <row>
      <xsl:apply-templates select="CELL"/>
    </row>
  </xsl:template>

  <xsl:template match="CELL">
    <entry>
      <xsl:if test="@COLSPAN > 1">
        <xsl:attribute name="namest">
          <xsl:value-of select="concat('c', sum(preceding-sibling::CELL/@COLSPAN) + 1)"/>
        </xsl:attribute>

        <xsl:attribute name="nameend">
          <xsl:value-of select="concat('c', sum(preceding-sibling::CELL/@COLSPAN) + @COLSPAN)"/>
        </xsl:attribute>
      </xsl:if>

      <xsl:value-of select="."/>
    </entry>
  </xsl:template>

</xsl:stylesheet>

Output:

<row>
  <entry>1</entry>
  <entry>Mandrel</entry>
  <entry>1</entry>
  <entry namest="c4" nameend="c5">H079249303</entry>
  <entry namest="c6" nameend="c7">H079249301</entry>
</row>
0

精彩评论

暂无评论...
验证码 换一张
取 消