[xsl] InDesign IDML table conversion

Subject: [xsl] InDesign IDML table conversion
From: Ganesh Babu N <nbabuganesh@xxxxxxxxx>
Date: Fri, 17 Aug 2012 18:41:50 +0530
Hi All,

I am trying to convert to CALS table to IDML (InDesign Markup
Language) table. As a first step I am merging html table model with
cals table attributes so that we can view the correct input.

I am facing difficulty in modifying the cell value when morerows has
been given. Here is my xml
<table>
				<tbody>
					<tr>
						<td>Alcohol</td>
						<td>82</td>
						<td>65</td>
						<td>3.6</td>
					</tr>
					<tr>
						<td morerows="1" rowspan="2">Cigarettes</td>
						<td>NA</td>
						<td morerows="1" rowspan="2">test</td>
						<td>7.6</td>
					</tr>
					<tr>
						<td>47</td>
						<td>4.4</td>
					</tr>
					<tr>
						<td morerows="1" rowspan="2">Inhalants</td>
						<td morerows="1" rowspan="2">6</td>
						<td morerows="1" rowspan="2">0.5</td>
						<td>0.0</td>
					</tr>
					<tr>
						<td>0.0</td>
					</tr>
</tbody>
</table>

Expected output:

<Table Self="d1e4"
       HeaderRowCount="2"
       FooterRowCount="0"
       BodyRowCount="8"
       ColumnCount="4"
       AppliedTableStyle="TableStyle/$ID/[Basic Table]"
       TableDirection="LeftToRightDirection">
   <Column Self="d36e3" Name="0" SingleColumnWidth="25"/>
   <Column Self="d36e3" Name="1" SingleColumnWidth="25"/>
   <Column Self="d36e3" Name="2" SingleColumnWidth="25"/>
   <Column Self="d36e3" Name="3" SingleColumnWidth="25"/>
   <Cell Self="d1e25" Name="2:0" RowSpan="1" ColumnSpan="1">Alcohol</Cell>
   <Cell Self="d1e27" Name="2:1" RowSpan="1" ColumnSpan="1">82</Cell>
   <Cell Self="d1e29" Name="2:2" RowSpan="1" ColumnSpan="1">65</Cell>
   <Cell Self="d1e31" Name="2:3" RowSpan="1" ColumnSpan="1">3.6</Cell>
   <Cell Self="d1e34" Name="3:0" RowSpan="2" ColumnSpan="1">Cigarettes</Cell>
   <Cell Self="d1e36" Name="3:1" RowSpan="1" ColumnSpan="1">NA</Cell>
   <Cell Self="d1e38" Name="3:2" RowSpan="2" ColumnSpan="1">test</Cell>
   <Cell Self="d1e40" Name="3:3" RowSpan="1" ColumnSpan="1">7.6</Cell>
   <Cell Self="d1e43" Name="4:1" RowSpan="1" ColumnSpan="1">47</Cell>
   <Cell Self="d1e45" Name="4.3" RowSpan="1" ColumnSpan="1">4.4</Cell>
   <Cell Self="d1e48" Name="5:0" RowSpan="2" ColumnSpan="1">Inhalants</Cell>
   <Cell Self="d1e50" Name="5:1" RowSpan="2" ColumnSpan="1">6</Cell>
   <Cell Self="d1e52" Name="5:2" RowSpan="2" ColumnSpan="1">0.5</Cell>
   <Cell Self="d1e54" Name="5:3" RowSpan="1" ColumnSpan="1">0.0</Cell>
   <Cell Self="d1e57" Name="6:3" RowSpan="1" ColumnSpan="1">0.0</Cell>
</Table>

I am trying to find out the number tds before the td[@morerows], so
that i can increase the cell count accordingly. But it is not giving
the exact number of tds. For the first set it should give 0 and 2 and
for the 2nd set it should give 3 . But it is giving 2 everywhere.
Please suggest me where I am going wrong.

Here is my style sheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
	<xsl:strip-space elements="*"/>
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="tgroup">
		<Table>
			<xsl:attribute name="Self">
				<xsl:value-of select="generate-id()"/>
			</xsl:attribute>
			<xsl:attribute name="HeaderRowCount">
				<xsl:value-of select="count(thead/tr)"/>
			</xsl:attribute>
			<xsl:attribute name="FooterRowCount">0</xsl:attribute>
			<xsl:attribute name="BodyRowCount">
				<xsl:value-of select="count(tbody/tr)"/>
			</xsl:attribute>
			<xsl:attribute name="ColumnCount">
				<xsl:value-of select="@cols"/>
			</xsl:attribute >
			<xsl:attribute name="AppliedTableStyle">TableStyle/$ID/[Basic
Table]</xsl:attribute>
			<xsl:attribute name="TableDirection">LeftToRightDirection</xsl:attribute>
			<!-- ForView -->
			<xsl:apply-templates/>
		</Table>
	</xsl:template>
	<xsl:template match="colspec">
		<Column Self="d36e3" Name="{@colnum - 1}" SingleColumnWidth="25"/>
	</xsl:template>
	<xsl:template match="tr">
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="th|td">
		<xsl:variable name="hrow">
			<xsl:choose>
				<xsl:when test="name()='th'">0
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="count(ancestor::tgroup/thead/tr)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="trpos">
			<xsl:value-of select="count(parent::tr/preceding-sibling::tr) + $hrow"/>
		</xsl:variable>
		<xsl:variable name="colspan">
			<xsl:choose>
				<xsl:when test="@namest">
					<xsl:value-of select="number(substring-after(@nameend,'col')) -
number(substring-after(@namest,'col')) + 1"/>
				</xsl:when>
				<xsl:otherwise>1</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="rowspan">
			<xsl:choose>
				<xsl:when test="@morerows">
					<xsl:value-of select="@morerows + 1"/>
				</xsl:when>
				<xsl:otherwise>1</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<Cell>
			<xsl:attribute name="Self">
				<xsl:value-of select="generate-id()"/>
			</xsl:attribute>
			<xsl:attribute name="Name">
				<xsl:choose>
					<xsl:when test="preceding-sibling::td[@namest] or
preceding-sibling::th[@namest]">
						<xsl:value-of select="concat($trpos,':',position()-1+$colspan)"/>
					</xsl:when>
					<xsl:when test="(@namest and not(following-sibling::td)) and
(not(following-sibling::th) and @namest)">
						<xsl:value-of select="concat($trpos,':',position() - 1 +
($colspan - 1))"/>
					</xsl:when>
					<xsl:when test="parent::tr/preceding-sibling::tr[1]/td[@morerows]">
						<xsl:value-of
select="concat($trpos,':',count(parent::tr/preceding-sibling::tr[1]/td[@morerows]/preceding-sibling::td))"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="concat($trpos,':',position()-1)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:attribute name="RowSpan">
				<xsl:value-of select="$rowspan"/>
			</xsl:attribute>
			<xsl:attribute name="ColumnSpan">
				<xsl:value-of select="$colspan"/>
			</xsl:attribute>
			<xsl:apply-templates/>
		</Cell>
	</xsl:template>
</xsl:stylesheet>

Here is the output of this code.

<Table Self="d1e4"
       HeaderRowCount="2"
       FooterRowCount="0"
       BodyRowCount="8"
       ColumnCount="4"
       AppliedTableStyle="TableStyle/$ID/[Basic Table]"
       TableDirection="LeftToRightDirection">
   <Column Self="d36e3" Name="0" SingleColumnWidth="25"/>
   <Column Self="d36e3" Name="1" SingleColumnWidth="25"/>
   <Column Self="d36e3" Name="2" SingleColumnWidth="25"/>
   <Column Self="d36e3" Name="3" SingleColumnWidth="25"/>
   <Cell Self="d1e25" Name="2:0" RowSpan="1" ColumnSpan="1">Alcohol</Cell>
   <Cell Self="d1e27" Name="2:1" RowSpan="1" ColumnSpan="1">82</Cell>
   <Cell Self="d1e29" Name="2:2" RowSpan="1" ColumnSpan="1">65</Cell>
   <Cell Self="d1e31" Name="2:3" RowSpan="1" ColumnSpan="1">3.6</Cell>
   <Cell Self="d1e34" Name="3:0" RowSpan="2" ColumnSpan="1">Cigarettes</Cell>
   <Cell Self="d1e36" Name="3:1" RowSpan="1" ColumnSpan="1">NA</Cell>
   <Cell Self="d1e38" Name="3:2" RowSpan="2" ColumnSpan="1">test</Cell>
   <Cell Self="d1e40" Name="3:3" RowSpan="1" ColumnSpan="1">7.6</Cell>
   <Cell Self="d1e43" Name="4:2" RowSpan="1" ColumnSpan="1">47</Cell>
   <Cell Self="d1e45" Name="4:2" RowSpan="1" ColumnSpan="1">4.4</Cell>
   <Cell Self="d1e48" Name="5:0" RowSpan="2" ColumnSpan="1">Inhalants</Cell>
   <Cell Self="d1e50" Name="5:1" RowSpan="2" ColumnSpan="1">6</Cell>
   <Cell Self="d1e52" Name="5:2" RowSpan="2" ColumnSpan="1">0.5</Cell>
   <Cell Self="d1e54" Name="5:3" RowSpan="1" ColumnSpan="1">0.0</Cell>
   <Cell Self="d1e57" Name="6:2" RowSpan="1" ColumnSpan="1">0.0</Cell>
</Table>

Regards,
Ganesh

Current Thread