xsl-list
[Top] [All Lists]

Re: [xsl] OOXML tables and CALS tables

2010-03-23 10:26:31
Dear Nic,

Please have a look and see if you can use anything of the following. It's based on DocBook XSL roundtrip (wich is XSLT 1.0, therefore there are two named templates instead of functions, for historical reasons), but slightly improved.

<!-- (w:tbl and w:tr translate to tgroup and row straightforwardly; not included here) -->

<!-- w:vMerge without Attribute means: a cell that is occupied by virtue of a row spanning cell in the preceding row, and which should be suppressed in CALS: -->
  <xsl:template match="w:tc[not(w:tcPr/w:vMerge[count(@*) = 0])]">
    <xsl:element name="entry">
      <!-- Process any spans -->
      <xsl:call-template name="cell.span"/>
      <xsl:call-template name="cell.morerows"/>
<!-- <xsl:call-template name="cell.style"/> --><!-- (you didn't ask for styles) -->
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>

  <!-- recursively count preceding columns, including spans -->
<xsl:function name="letex:colcount" as="xs:double" saxon:memo-function="yes">
    <xsl:param name="count" as="xs:double"/>
    <xsl:param name="cell"  as="node()"/>

    <xsl:choose>
      <xsl:when test="$cell/preceding-sibling::w:tc">
<xsl:variable name="span" select="if ($cell/preceding-sibling::w:tc[1]/w:tcPr/w:gridSpan/@w:val) then $cell/preceding-sibling::w:tc[1]/w:tcPr/w:gridSpan/@w:val
                                          else 1"/>
<xsl:sequence select="letex:colcount($count + $span, $cell/preceding-sibling::w:tc[1])" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="$count"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>


  <xsl:template name="cell.span">
    <xsl:variable name="span" select="0 + w:tcPr/w:gridSpan/@w:val"/>
    <xsl:if test="$span &gt; 0">
      <!-- Get the current cell number -->
      <xsl:variable name="colstart" select="letex:colcount(1, .)" />
      <xsl:attribute name="namest">
        <xsl:value-of  select="concat('col', $colstart)"/>
      </xsl:attribute>
      <xsl:attribute name="nameend">
        <xsl:value-of  select="concat('col', $colstart + $span - 1)"/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

<!-- The trickiest part: -->
  <xsl:template name="cell.morerows">
    <xsl:if test="w:tcPr/w:vMerge/@w:val = 'restart'">
      <xsl:variable name="counts" as="xs:integer*">
        <xsl:choose>
<xsl:when test="../following-sibling::w:tr[1]/w:tc[letex:colcount(1, .) = letex:colcount(1, current())]/w:tcPr/w:vMerge[count(@*) = 0]"> <xsl:for-each-group select="../following-sibling::w:tr/w:tc[letex:colcount(1, .) = letex:colcount(1, current())]" group-adjacent="if (w:tcPr/w:vMerge[count(@*) = 0]) then true() else false()">
              <xsl:sequence select="count(current-group())"/>
            </xsl:for-each-group>
          </xsl:when>
          <xsl:otherwise>
<!-- shouldn't happen, need to fix the code: -->
            <xsl:sequence select="24"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:attribute name="morerows" select="$counts[1]" />
      <xsl:attribute name="style" select="'border-bottom: none;'" />
    </xsl:if>
  </xsl:template>

I should have taken care of the "otherwise return 24" above. But I'm staying sick at home, and all I could do is copy and paste this code that I once wrote. I'm unable to think about it right now. Hope this helps anyway.

Gerrit


On 23.03.2010 13:07, Nic Gibson wrote:
Morning

This is a sort "on the off chance" query. I'm just about to sit down and write 
XSLT to convert some tables created in Word and saved to docx files to CALS tables (in 
DocBook).  I don't actually need to replicate anything much on the formatting side except 
the row and column spanning cells (merged in Word terminology). I was wondering if anyone 
has actually done this already and would be willing to share the XSLT they used.

cheers

nic
--
Nic Gibson
Director, Corbas Consulting Ltd
Editorial and Technical Consultancy and Training
http://www.corbas.co.uk, +44 (0)7718 906817     





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--
Gerrit Imsieke
Geschäftsführer / Managing Director
le-tex publishing services GmbH
Weissenfelser Str. 84, 04229 Leipzig, Germany
Phone +49 341 355356 110, Fax +49 341 355356 510
gerrit(_dot_)imsieke(_at_)le-tex(_dot_)de, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschäftsführer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vöckler

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>