【发布时间】:2019-07-24 18:07:21
【问题描述】:
我有一个从 Excel 导出的 XML,我需要将它(使用 XSLT1.0)转换为 Adobe InDesign 可以读取的另一个 XML。我已经能够拆分成不同的表并生成可接受的 XML,InDesign 可以轻松读取。我现在需要指定给定表格上每行的最大非空单元格数,或者 - 更容易 - 提供给定行上最后一个非空单元格的位置。
到目前为止,我一直在使用以下代码:
<xsl:value-of select="count(Row[2]/Cell/*[not(*)])"></xsl:value-of>
它返回每个表的第二行(始终是表中最长的行)上非空单元格的数量。问题是有时我需要跳过第一列或第二列以保持对齐,例如我得到的值不是 3,而是 2。
这是我需要解析的xml:
<Table name="Configs">
<Row>
<Cell StyleID="s73"><Data Type="String">Configs</Data></Cell>
<Cell StyleID="s73"/> <!-- this must be empty AND visible -->
<Cell StyleID="s73"/> <!-- this must be empty AND visible -->
<Cell StyleID="s64"/> <!-- this must be REMOVED in final xml -->
<Cell StyleID="s64"/> <!-- this must be REMOVED in final xml -->
<Cell StyleID="s62"/> <!-- this must be REMOVED in final xml -->
</Row>
<Row>
<Cell StyleID="s62"><Data Type="String">This</Data></Cell>
<Cell StyleID="s62"/> <!-- this must be empty AND visible -->
<Cell StyleID="s64"><Data Type="String">That</Data></Cell>
<Cell StyleID="s64"/> <!-- this must be REMOVED in final xml -->
<Cell StyleID="s64"/> <!-- this must be REMOVED in final xml -->
<Cell StyleID="s62"/> <!-- this must be REMOVED in final xml -->
</Row>
<Row>
<Cell StyleID="s62"><Data Type="String">A1</Data></Cell>
<Cell StyleID="s62"><Data Type="String">B1</Data></Cell>
<Cell StyleID="s64"><Data Type="String">C1</Data></Cell>
<Cell StyleID="s64"/> <!-- this must be REMOVED in final xml -->
<Cell StyleID="s64"/> <!-- this must be REMOVED in final xml -->
<Cell StyleID="s62"/> <!-- this must be REMOVED in final xml -->
</Row>
<Row>
<Cell StyleID="s62"><Data Type="String">A2</Data></Cell>
<Cell StyleID="s62"><Data Type="String">B2</Data></Cell>
<Cell StyleID="s64"><Data Type="String">C2</Data></Cell>
<Cell StyleID="s64"/> <!-- this must be REMOVED in final xml -->
<Cell StyleID="s64"/> <!-- this must be REMOVED in final xml -->
<Cell StyleID="s62"/> <!-- this must be REMOVED in final xml -->
</Row>
</Table>
这是我正在使用的 XSL 的(一部分)
<xsl:if test="count(Row) > 1">
<xsl:attribute name="Id:tcols" namespace="http://ns.adobe.com/AdobeInDesign/4.0/">
<!-- <xsl:value-of select="count(Row[2]/Cell/*[not(*)])"></xsl:value-of> -->
<xsl:value-of select="count(Row[2]/Cell/*[not(*)])"></xsl:value-of>
</xsl:attribute>
<xsl:variable name="columns-in-table" select="count(Row[2]/Cell/*[not(*)])"></xsl:variable>
<xsl:for-each select="Row">
<xsl:for-each select="Cell">
<xsl:if test="position() < $columns-in-table + 1">
<xsl:element name="Cell">
<xsl:attribute name="Id:table" namespace="http://ns.adobe.com/AdobeInDesign/4.0/">cell</xsl:attribute>
<xsl:attribute name="Id:crows" namespace="http://ns.adobe.com/AdobeInDesign/4.0/">1</xsl:attribute>
<xsl:attribute name="Id:ccols" namespace="http://ns.adobe.com/AdobeInDesign/4.0/">1</xsl:attribute>
<xsl:value-of select="Data"></xsl:value-of>
</xsl:element> <!-- /Cell -->
</xsl:if>
</xsl:for-each> <!-- /select Cell-->
</xsl:for-each> <!-- /select Row-->
</xsl:if>
我需要得到的是:
<Table xmlns:Id="http://ns.adobe.com/AdobeInDesign/4.0/" Name="Configs" Id:table="table" Id:trows="4" Id:tcols="3">
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">Configs</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1"/>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1"/>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">This</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1"/>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">That</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">A1</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">B1</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">C1</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">A2</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">B2</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">C2</Cell>
</Table>
我得到的是:
<Table xmlns:Id="http://ns.adobe.com/AdobeInDesign/4.0/" Name="Configs" Id:table="table" Id:trows="4" Id:tcols="2">
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">Configs</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1"/>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">This</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1"/>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">A1</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">B1</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">A2</Cell>
<Cell Id:table="cell" Id:crows="1" Id:ccols="1">B2</Cell>
</Table>
【问题讨论】:
-
我认为只有改变:
-
$columns-in-table 也会返回错误的列数,因为它对 Id:tcols 使用完全相同的代码