1

This is an offshoot of of this question together with an attempt to use this answer - but something went wrong...

This is the code:

 <table>
       <row>
           <column/>
       </row>
       <row>
           <column/>
           <column/>
           <column/>
       </row>
       <row>
           <column/>
           <column/>
       </row>  
</table>

The target is the <row> with the maximum number of <column> children.

The expression

./table/row/count(column)

correctly outpust

1,3,2

The expression

max(./table/row/count(column))

correctly outputs

3

The expression

./table/row[count(column) = 3]

correctly selects the target row. BUT - the expression

./table/row[count(column) = max(./table/row/count(column))]

results in no matches.

Am I using this incorrectly or is it just the way thing are?

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45

3 Answers3

1

In your XPath expression, your context item in the predicate is ./table/row. So count(column) relates to the current node and succeeds, because column is a child of row. Your max(...) expression also relates to this context item row, but it has no ./table/row/count(column) child and so it returns empty.

Going back two levels using a relative expression like this does solve this problem:

./table/row[count(column) = max(../../table/row/count(column))]

Otherwise, if you want to use an absolute path, you can also use

./table/row[count(column) = max(/table/row/count(column))]

The result is the same in both cases (for your example).

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
zx485
  • 28,498
  • 28
  • 50
  • 59
1

You are almost there... the only thing that caused the issue here is the scope.

Here is the correct xpath.

./table/row[count(column) = max(/table/row/count(column))]

In your original xpath "./table/row[count(column) = max(./table/row/count(column))]" , the dot in the max is nothing but limiting the scope to the current node i.e ./table/row in your case. As you have you to check in all the table rows to findout the max columns, you should remove the . in the max function.

supputuri
  • 13,644
  • 2
  • 21
  • 39
1

For better readability, and probably faster evaluation, use the full power of XPath 2.0:

 for $vMax in max(/table/row/count(column))
    return
       /table/row[count(column) eq $vMax]
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • I really, really wish I could use XPath 2.0 (or 3.1, for that matter), but to quote @Michael Kay in one of his answers "Sadly, 12 years after XPath 2.0 was published, and despite all the known limitations of XPath 1.0, the browser vendors have still not updated their XPath implementations"; and the same goes for many python libraries (lxml, for example).... – Jack Fleeting Jun 11 '19 at 12:30
  • @JackFleeting, I am both able and willing to help browser companies transition to at least XPath 2.0 -- if the interest was recognized by them, we could do it. – Dimitre Novatchev Jun 11 '19 at 14:53
  • Let's hope someone out there is reading this (and is in a position to actually take action), because it's sorely needed. I really don't understand why something important like this is being neglected. – Jack Fleeting Jun 11 '19 at 16:45