开发者

XPath - determine the element position

开发者 https://www.devze.com 2023-04-02 03:38 出处:网络
I want to create an index (determine the position in the XML) for every table but the 开发者_开发知识库problem is that the tables are in different depth. I plan to process the XML with XSLT transforma

I want to create an index (determine the position in the XML) for every table but the 开发者_开发知识库problem is that the tables are in different depth. I plan to process the XML with XSLT transformation to FO. I Any ideas how to do this?

Sample XML

<document>
    <table> ... </table>

    <section>
        <table> ... </table>

        <subsection>
            <table> ... </table>
        </subsection>
    </section>
</document>


@Tomalak's solution isn't completely correct and will produce wrong result in the case when there are nested tables.

The reason for this is that the XPath preceding and ancestor axes are non-overlapping.

One correct XPath expression that gives the wanted number is:

count(ancestor::table | preceding::table) + 1

So, use:

<xsl:template match="table">
    <table id="tbl_{count(ancestor::table | preceding::table) + 1}">
        <!-- further processing -->
    </table>
</xsl:template>


This will number your tables consecutively, starting from 1.

<xsl:template match="table">
  <table id="tbl_{count(preceding::table) + 1}">
    <!-- further processing -->
  </table>
</xsl:template>
0

精彩评论

暂无评论...
验证码 换一张
取 消