I want to transform Indesign table XML converts to HTML but I am not getting the correct output and need to handle dynamically aid:crows and aid:ccols. can anyone please suggest the correct table HTML output?
Input XML
<DOCUMENT xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
<table-wrap aid:pstyle="table-wrap">
<table aid:table="table" aid5:tablestyle="maintable" aid:trows="7" aid:tcols="4">
<Cell aid:table="cell" aid5:cellstyle="TCH-l"><p aid:pstyle="TB">Fruit Part</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TCH-l" aid:ccols="2"><p aid:pstyle="TB">Variety/Color</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TCH-l"><p aid:pstyle="TB">Reference</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB-f" aid:crows="3"><p aid:pstyle="TB"><B aid:cstyle="CELL_B">Peel</B></p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB-f" aid:crows="3"><p aid:pstyle="TB">Orange</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB-f"><p aid:pstyle="TB">A1</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB-f" aid:crows="3"><p aid:pstyle="TB">A2</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="TB">A3</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="TB">A4</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="TB"><B aid:cstyle="CELL_B">Pulp</B></p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB" aid:crows="2"><p aid:pstyle="TB">Orange/yellow</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="TB">A5</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB" aid:crows="2"><p aid:pstyle="TB">A6</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="P"></p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="TB">A7</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="P"></p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="TB">Red/yellow/orange</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="TB">A8</p></Cell>
<Cell aid:table="cell" aid5:cellstyle="TB"><p aid:pstyle="TB">A9</p></Cell>
</table>
</table-wrap>
Required HTML output:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<table border="1">
<tbody>
<tr>
<td class="TCH-l"><p>Fruit Part</p></td>
<td class="TCH-l" colspan="2"><p>Variety/Color</p></td>
<td class="TCH-l"><p>Reference</p></td>
</tr>
<tr>
<td class="TB-f" rowspan="3"><p><strong>Peel</strong></p></td>
<td class="TB-f" rowspan="3"><p>Orange</p></td>
<td class="TB-f"><p>A1</p></td>
<td class="TB-f" rowspan="3"><p>A2</p></td>
</tr>
<tr>
<td class="TB"><p>A3</p></td>
</tr>
<tr>
<td class="TB"><p>A4</p></td>
</tr>
<tr>
<td class="TB"><p><strong>Pulp</strong></p></td>
<td class="TB" rowspan="2"><p>Orange/yellow</p></td>
<td class="TB"><p>A5</p></td>
<td class="TB" rowspan="2"><p>A6</p></td>
</tr>
<tr>
<td class="TB"><p></p></td>
<td class="TB"><p>A7</p></td>
</tr>
<tr>
<td class="TB"><p></p></td>
<td class="TB"><p>Red/yellow/orange</p></td>
<td class="TB"><p>A8</p></td>
<td class="TB"><p>A9</p></td>
</tr>
</tbody>
</table>
</body>
</html>
XSLT code:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="aid aid5 xs">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<table border="1"><tbody>
<xsl:apply-templates select="//table"/>
</tbody></table>
</body>
</html>
</xsl:template>
<xsl:template match="table">
<!-- FIX: force integer -->
<xsl:variable name="rows" select="xs:integer(@aid:trows)"/>
<xsl:variable name="cols" select="xs:integer(@aid:tcols)"/>
<xsl:variable name="cells" select="Cell"/>
<!-- Loop rows -->
<xsl:for-each select="1 to $rows">
<tr>
<xsl:variable name="current-row" select="."/>
<!-- Loop cells in input order -->
<xsl:for-each select="$cells">
<xsl:variable name="pos" select="position()"/>
<!-- Calculate starting row for this cell -->
<xsl:variable name="start-row"
select="1 +
count(
$cells[
position() lt $pos
and @aid:crows
]
)"/>
<!-- If this cell belongs to this row, output it -->
<xsl:if test="$start-row = $current-row">
<xsl:apply-templates select="."/>
</xsl:if>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template match="Cell">
<td>
<xsl:attribute name="class">
<xsl:value-of select="@aid5:cellstyle"/>
</xsl:attribute>
<xsl:if test="@aid:ccols">
<xsl:attribute name="colspan">
<xsl:value-of select="@aid:ccols"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@aid:crows">
<xsl:attribute name="rowspan">
<xsl:value-of select="@aid:crows"/>
</xsl:attribute>
</xsl:if>
<p><xsl:apply-templates select="p/node()"/></p>
</td>
</xsl:template>
<xsl:template match="B">
<strong><xsl:apply-templates/></strong>
</xsl:template>
</xsl:stylesheet>
<Cell>elements) into a standard HTML table. Some cells span multiple rows or columns. I need help understanding the logic to detect these spans and correctly generate the corresponding rowspan and colspan in HTML. That’s what I meant by ‘handle dynamically rowspan and colspan’.