首页 > 解决方案 > 使用 XSL 将 TEI-XML 字典转换为简单的 HTML 表

问题描述

需要把“entryFree”字放在第一列(<form>内容留第二列),“sense”等作为第二列,每对在同一行,有边框。示例 XSL 样式表仅包含格式。

示例 XML:https ://drive.google.com/file/d/1sNAbWw5xo1pgwK2QfQwPrbZtZt8uV48T/view?usp=sharing

花式 XSL(许可证允许修改):https ://github.com/michmech/tei-dictionary.xsl

标签: xmlxslttei

解决方案


如果要将所有entryFree元素映射到 HTML 表格行(即 HTMLtr中的 HTML 元素table),则table根据需要设置所有元素,并在一个tbody过程中将entryFree它们与模板映射到 a tr

<xsl:output method="html" doctype-system="about:legacy-doctype"/>

<xsl:template match="/">
    <html>
        <head>
            <title>Test</title>
        </head>
        <body>
            <h1>Table</h1>
            <table>
                <thead>
                    <tr>
                        <th>free entry</th>
                        <th>forms/senses</th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:apply-templates select="//tei:entryFree"/>
                </tbody>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="tei:entryFree">
    <tr>
        <td>
            <xsl:value-of select="@sortKey"/>
        </td>
        <td>
           <xsl:apply-templates/> 
        </td>
    </tr>
</xsl:template>

tei:entryFree您显然需要删除模板匹配。

至于格式化表格,这是一个 HTML/CSS 问题,HTML 4 https://www.w3.org/TR/html401/struct/tables.html#h-11.3.1允许例如

            <table rules="all" frame="border">
                <thead>
                    <tr>
                        <th>free entry</th>
                        <th>forms/senses</th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:apply-templates select="//tei:entryFree"/>
                </tbody>
            </table>

在 HTML5 中,我认为使用 CSS 是首选:

<xsl:template match="/">
    <html>
        <head>
            <title>Test</title>
            <style>
                table.dict { 
                  border: 1px solid black;
                  border-collapse: collapse;
                }
                table.dict th, table.dict td {
                  border: 1px solid black;
                }
            </style>
        </head>
        <body>
            <h1>Table</h1>
            <table class="dict">
                <thead>
                    <tr>
                        <th>free entry</th>
                        <th>forms/senses</th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:apply-templates select="//tei:entryFree"/>
                </tbody>
            </table>
        </body>
    </html>
</xsl:template>

推荐阅读