htmlxmlxsltxsl-folist-definition

Transform an HTML DL into 2-column XSL-FO table of terms and definitions


How can I use XSLT to turn an HTML definition list (DL) into a two-column XSL-FO table with the terms in the first column and the definitions in the second, with multiple definitions possible?

(Apologies if this is already answered somewhere. I searched but did not find.)


Solution

  • This question has the crux of finding the following-siblings that still have the current DT as their term: XSLT: Select following-sibling until reaching a specified tag

    Then it's just a matter of defining the whole table as the top-level DL template:

    <xsl:template match="dl">
        <fo:table>
            <fo:table-body>
                <xsl:for-each select="dt">
                    <xsl:variable name="current-term" select="."/>
                    <fo:table-row>
                        <fo:table-cell font-weight="bold" width="5.5cm">
                            <fo:block>
                                <xsl:apply-templates />
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                <xsl:apply-templates select="following-sibling::dd[preceding-sibling::dt[1] = $current-term]" />
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </xsl:for-each>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <xsl:template match="dd">
        <fo:block>
            <xsl:apply-templates />
        </fo:block>
    </xsl:template>