xpath icon indicating copy to clipboard operation
xpath copied to clipboard

How to "zip" two lists together?

Open jf-tech opened this issue 2 years ago • 1 comments

Say I have a doc like this:

<root>
   <NAMES>
     <name>Wang</name>
     <name>Joe</name>
     <name>Ali</name>
   </NAMES>
   <AGES>
     <age>10</age>
     <age>20</age>
     <age>17</age>
   </AGES>
</root>

I want to find out the age of a person whose name is Joe. So essentially I need to zip two lists <NAMES> and <AGES> together, based on position(). I can't figure out a way to write this query:

/root/AGES/age[ position() = ../../NAMES/name[text() = 'Joe']::position() ]

Basically I want to get an <age> node whose position() equals the position() of a <name> node whose value is Joe. I have a strong assumption and guarantee that the numbers of elements in the <NAMES> and <AGES> lists are the same, so no need to worry about that.

The query doesn't work, the error basically says: has an invalid token.

Any insight/idea how to achieve this? Would really like to avoid doing the zip processing in code.

Thanks!!

jf-tech avatar May 11 '23 21:05 jf-tech

/root/AGES/age[position() = ../../NAMES/name[text() = 'Joe']/position()] expr is correct that shoud be able to work in antoher languages but not in xpath package.

I mark it as bug or enhancement.

zhengchun avatar May 12 '23 16:05 zhengchun