XSL 将简单 HTML table 转换为 InDesign Table(用于 XML 导入)

XSL to transform simple HTML table to InDesign Table (for XML import)

我想知道这里是否有人需要将简单的 HTML table(在将导入到 InDesign 中的 XML 中)转换为 InDesign table ,像这样:

这是输入(简化):

<?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: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" />

<xsl:template match="table">
    <Tabla aid:table="table" aid5:tablestyle="tabla">
        <xsl:attribute name="aid:tcols">
          <xsl:for-each select=".//tr">
            <xsl:sort select="count(td)" order="descending"/>
            <xsl:if test="position() = 1">
              <xsl:value-of select="count(td)"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:attribute>
        <xsl:attribute name="aid:trows">
          <xsl:for-each select=".//tbody">
            <xsl:sort select="count(tr)" order="descending"/>
            <xsl:if test="position() = 1">
              <xsl:value-of select="count(tr)"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </Tabla>
</xsl:template>

<xsl:template match="th | tr | td | thead | tbody">
        <Cell>
            <xsl:apply-templates select="node()[not(self::th | self::tr | self::td | self::thead | self::tbody)]"/>
        </Cell>
        <xsl:apply-templates select="th | tr | td " />
</xsl:template>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

但这会产生重复的标签,而且可能不正确:

<Table aid:table="table" aid5:tablestyle="tabla" aid:tcols="3" aid:trows="4" border="1" cellspacing="1" cellpadding="1" style="width:500px">
  <Cell />
  <Cell />
  <Cell>asa</Cell>
  <Cell>asdsd</Cell>
  <Cell>asdads</Cell>
  <Cell />
  <Cell />
  <Cell>d</Cell>
  <Cell>dd</Cell>
  <Cell>ddd</Cell>
  <Cell />
  <Cell>d</Cell>
  <Cell>dd</Cell>
  <Cell>ddd</Cell>
  <Cell />
  <Cell>d</Cell>
  <Cell>dd</Cell>
  <Cell>ddd</Cell>
  <Cell />
  <Cell>d</Cell>
  <Cell>dd</Cell>
  <Cell>ddd</Cell>
</Table>

到目前为止,最困难的部分是“扁平化”Table 层次结构以匹配 InDesign 的预期,我已经很久没有使用 XSL(因为我是 Coldfusion 程序员)并且老实说忘记了所有关于它。

希望这里有人能帮忙,谢谢。

能不能简单点:

XSLT 1.0

<xsl:stylesheet version="1.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/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
<xsl:template match="/publication">
    <Table aid5:tablestyle="custom_table" style="width:500px" cellpadding="1" cellspacing="1" border="1" aid:table="table" aid:trows="{count(text/table/*/tr)}" aid:tcols="{count(text/table/thead/tr/th)}">
        <xsl:for-each select="text/table/*/tr/*">
            <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="40">
                <xsl:value-of select="."/>
            </Cell>
        </xsl:for-each>
    </Table>
</xsl:template>

</xsl:stylesheet>