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

时间:2019-07-24 09:05:49

标签: xml xslt-1.0

我有一个从Excel导出的XML,我需要将其转换(使用XSLT1.0)成Adobe InDesign可以读取的另一个XML。我已经能够拆分成不同的表并生成一个InDesign可以轻松读取的可接受的XML。现在,我需要指定给定表中每行​​的非空单元格的最大数量,或者-更容易地-提供给定行中最后一个非空单元格的位置。

现在,我一直在使用以下代码:

<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) &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>

1 个答案:

答案 0 :(得分:0)

怎么样:

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