如何获取给定元素的最后一个非空子元素的位置

How to get position of last, non-empty child of given element

我有一个从 Excel 导出的 XML,我需要将它(使用 XSLT1.0)转换为另一个 Adob​​e InDesign 可以读取的 XML。我已经能够拆分成不同的 tables 并生成 InDesign 易于阅读的 acceptable XML。我现在需要指定给定 table 上每行的最大非空单元格数,或者 - 更容易地 - 提供给定行上最后一个非空单元格的位置。

到目前为止,我一直在使用以下代码:

<xsl:value-of select="count(Row[2]/Cell/*[not(*)])"></xsl:value-of>

其中 returns 每个 table 第二行的非空单元格数量(这始终是 table 中最长的一行)。问题是有时我需要跳过第一列或第二列以保持对齐,例如我得到的值不是 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) &gt; 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() &lt; $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>

怎么样:

<xsl:variable name="columns-in-table" select="count(Row[2]/Cell[*][last()]/preceding-sibling::Cell) + 1"/>