[xsl] Row/Col Span

Subject: [xsl] Row/Col Span
From: Cleyton Jordan <cleytonjordan@xxxxxxxxxxx>
Date: Thu, 7 May 2009 02:52:22 -0700 (PDT)
Hi,

I have written the XSLT below to figure out the number of rows and columns so
that I can add the rowspan and colspan to a <TD> element and it seems to be
working.

However, I am not sure if this is the best and most effective way to do it in
XSLT and would appreciate very much your comments and maybe if there is better
approach I would be thankful if you could share it with us.

Please note that I am using XSLT 1.0 and msxml

Given this xml:

1 - the colspan would be 2 (1 ColGrp + 1 Col)
2 - the rowsapn would be 2 (1 RowGrp + 1 Row)

<Report>
 <Columns>
    <ColGrp heading="Year">
      <Col heading="2003"/>
      <Col heading="2004"/>
    </ColGrp>
    <ColGrp heading="">
      <Col heading="Total"/>
    </ColGrp>
  </Columns>
  <Rows>
      <RowGrp heading="Journal Group Name">
        <Row heading="Gastroenterology">
          <Cell/>
          </Cell>
        </Row>
        <Row heading="Pharmacy">
          <Cell/>
          <Cell/>
        </Row>
      </RowGrp>
      <RowGrp heading="">
        <Row heading="Totals">
          <Cell/>
          <Cell/>
        </Row>
      </RowGrp>
  </Rows>
</Report>

I would like to point out that I can have many <ColGrp> elements child of
<ColGrp>. Similarly, I may have many <RowGrp> elements. Therefore, I need a
dynamic way to count all ColGrp/RowGrp + 1 for the Col/Row

3 - here the colspan would be 4 ( ColGrp + ColGrp + ColGrp + Col)

<Columns>
 <ColGrp heading="Test">
   <ColGrp heading="Test">
     <ColGrp heading="Year">
       <Col heading="2003"/>
       <Col heading="2004"/>
     </ColGrp>
     <ColGrp heading="">
       <Col heading="Total"/>
     </ColGrp>
   </ColGrp>
 </ColGrp>
</Columns>

+++++

4 - here the rowspan would be 3 (RowGrp + RowGrp + Row)

<Rows>
    <RowGrp heading="Media Full Name">
      <RowGrp heading="Oncology">
        <Row heading="Blood">
          <Cell/>
        </Row>
        <Row heading="Oncology News International">
          <Cell/>
        </Row>
      </RowGrp>
    </RowGrp>
    <RowGrp>
      <RowGrp>
        <Row heading="Total">
          <Cell/>
        </Row>
      </RowGrp>
    </RowGrp>
  </Rows>

 As you can see I need to work with only one ColGrp or RowGrp.

This is the extract of the XSLT I am using:

<xsl:variable name="row-span">
    <xsl:call-template name="count-row-span">
      <xsl:with-param name="set" select="/Report/Columns/ColGrp"/>
      <xsl:with-param name="rowcount" select="0"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="col-span">
    <xsl:call-template name="count-col-span">
      <xsl:with-param name="set" select="/Report/Rows/RowGrp"/>
      <xsl:with-param name="colcount" select="1"/>
    </xsl:call-template>
  </xsl:variable>

<xsl:template name="count-col-span">
    <xsl:param name="set"/>
    <xsl:param name="colcount"/>
    <xsl:choose>
      <xsl:when test="$set/RowGrp">
        <xsl:call-template name="count-col-span">
          <xsl:with-param name="set" select="$set/*[1]"/>
          <xsl:with-param name="colcount" select="$colcount + 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$set/Row">
        <xsl:value-of select ="$colcount + 1"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="count-row-span">
  <xsl:param name="set"/>
  <xsl:param name="rowcount"/>
  <xsl:choose>
    <xsl:when test="$set/ColGrp">
       <xsl:call-template name="count-row-span">
        <xsl:with-param name="set" select="$set/*[1]"/>
        <xsl:with-param name="rowcount" select="$rowcount + 1"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$set/Col">
      <xsl:value-of select ="count($set[1]/Col) + $rowcount + 2"/>
    </xsl:when>
  </xsl:choose>


Cheers

C

Current Thread