You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSL将HTML表格转换为适配InDesign XML导入的标准表格

HTML表格转InDesign兼容XML格式XSL实现

需要将待导入InDesign的XML文件中的简单HTML表格转换为InDesign可识别的扁平化表格格式,已编写的XSL运行后出现标签重复错误,无法符合输出要求。

简化版输入XML

<?xml version="1.0" encoding="UTF-8"?>
<publication xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
      <text>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        </p>
        <hr/>
        <table border="1" cellspacing="1" cellpadding="1" style="width:500px">
            <thead>
                <tr>
                    <th>Topic 1</th>
                    <th>Topic 2</th>
                    <th>Topic 3</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                </tr>
                <tr>
                    <td>11</td>
                    <td>22</td>
                    <td>33</td>
                </tr>
                <tr>
                    <td>111</td>
                    <td>222</td>
                    <td>3333</td>
                </tr>
                <tr>
                    <td>1111</td>
                    <td>2222</td>
                    <td>3333</td>
                </tr>
            </tbody>
        </table>
      </text>
</publication>

预期输出格式

<Table aid5:tablestyle="custom_table" style="width:500px" cellpadding="1" cellspacing="1" border="1" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table" aid:trows="5" aid:tcols="3">
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">Topic 1</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">Topic 2</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">Topic 3</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">1</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">2</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">3</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">11</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">22</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">33</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">111</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">222</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">333</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">1111</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">2222</Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">3333</Cell>
</Table>

现有XSL问题说明

原有XSL对thead/tbody/tr/th/td都配置了生成<Cell>的逻辑,导致出现大量空的重复Cell标签,同时行数统计未包含表头行,列数统计逻辑存在冗余。

修复后可用的XSL代码

<xsl:stylesheet 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"
exclude-result-prefixes="xs">

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<!-- 保留非表格元素的恒等转换 -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- 处理HTML table生成InDesign要求的Table标签 -->
<xsl:template match="table">
    <Table aid5:tablestyle="custom_table" aid:table="table">
        <!-- 复制原有table的属性:border、cellpadding、cellspacing、style等 -->
        <xsl:apply-templates select="@*"/>
        <!-- 计算总行数:表头行+表体行 -->
        <xsl:attribute name="aid:trows">
            <xsl:value-of select="count(.//tr)"/>
        </xsl:attribute>
        <!-- 计算总列数:取表头第一行的单元格数 -->
        <xsl:attribute name="aid:tcols">
            <xsl:value-of select="count(thead/tr[1]/th | tbody/tr[1]/td)"/>
        </xsl:attribute>
        <!-- 直接处理所有单元格,跳过中间层级实现扁平化 -->
        <xsl:apply-templates select=".//th | .//td"/>
    </Table>
</xsl:template>

<!-- 处理th/td生成Cell标签 -->
<xsl:template match="th | td">
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">
        <xsl:apply-templates select="node()"/>
    </Cell>
</xsl:template>

<!-- 跳过thead/tbody/tr层级,不生成任何标签,仅处理子节点 -->
<xsl:template match="thead | tbody | tr">
    <xsl:apply-templates select="node()"/>
</xsl:template>

</xsl:stylesheet>

代码说明

  • 跳过thead/tbody/tr中间层级,直接将所有th和td转换为同级的<Cell>标签,实现结构扁平化
  • 行数统计包含表头行,符合InDesign的表格参数要求
  • 自动复制原表格的border/cellpadding/cellspacing/style属性到输出的<Table>标签
  • 每个<Cell>默认配置固定属性,可根据实际需求调整aid:ccolwidth等参数值

内容的提问来源于stack exchange,提问作者dmm79

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 01:21:02