开发者

Xpath to get an 'id' or 'counter' of which loop number I'm on for a given xpath expression

Updated based on feedback, thanks -

I have an example such as below, where while parsing the xpath I would like to keep track of which row I'm on /rows/row via a number or identifier. I'm currently trying to transfer the XML into a relational format and have each set of columns identified with the 'Row ID',row number, or row position that it came from.

<rows>
<row>
<columns>
<column>
<name>x</name>
<value>val1.x</value>
</column>
<column>
<name>y</name>
<value>val1.y</value>
</column>
<column>
<name>z</name>
<value>val1.z</value>
</column>
</columns>
</row>
<row>
<columns>
<co开发者_运维百科lumn>
<name>x</name>
<value>val2.x</value>
</column>
<column>
<name>y</name>
<value>val2.y</value>
</column>
<column>
<name>z</name>
<value>val2.z</value>
</column>
</columns>
</row>
<row>
<column>
<name>x</name>
<value>val3.x</value>
</column>
<column>
<name>y</name>
<value>val3.y</value>
</column>
<column>
<name>z</name>
<value>val3.z</value>
</column>
</columns>
</row>
</rows>


Suppose your context node is a <value>, <name>, <column> or <columns> element and <row> elements are not in this document outside the current structure. Then the following XPath will give you the "row number"

count(ancestor::row) + count(ancestor::row[1]/preceding-sibling::row)

Indexing begins at 1. If the expression returns 0, the context node is not inside the row structure.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜