XPath 3.0: select nodes by condition (find the maximum child nodes by date)

Given XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <c cid="c0" v="100">
        <d did="c00" v="0" dt="2016-01-01" />
        <d did="c01" v="0" dt="2016-02-03" />
        <d did="c02" v="0" dt="2016-01-15" />
    </c>
    <c cid="c1" v="100">
        <d did="c10" v="1" dt="2016-01-01" />
        <d did="c11" v="0" dt="2015-03-03" />
        <d did="c12" v="0" dt="2015-04-15" />
    </c>
</root>

I need to find XPath 2.0 or 3.0, which will return me a list of nodes cby expression: find the maximum dnode of this cchild attribute dt(parse it as date), and if the attribute value vis "0", return cnode.

I expect to get one cnode (with @cid = "c0") as a result, because it is /root/c[cid="c1"]/d[@dt="2016-01-01"]/@vnot equal to 0 (equal to 1).

I am stuck in this XPath sofar:

/root/c[d[max(xs:date(@dt))]/@v="0"]

This gives me an error (I am using the Oxygen XML editor, XPath 3.0):

XPath error due to: Effective boolean value not defined for sequence starting with atomic value other than boolean, number, string or URI

, max, .

:

  • , c d dt.
  • XPath 3.0:

    /root/c[d[@dt=max(.//@dt/xs:date(.))]/@v="0"]

( ).

+4
2

, , ( <c> ) XPath 2.0 > :

/*/c[max(d/xs:date(@dt)) = d[@v eq '0']/@dt]

!

+2

XQuery c node:

for $c in /root/c
let $max := max($c/d/@dt/xs:date(.))
where $c/d[@dt = $max]/@v = '0'
return $c

XPath ( , ), d node , , :

/root/c[d[@dt = max(../d/@dt/xs:date(.))]/@v = '0']

let XPath :

/root/c[
  let $max := max(d/@dt/xs:date(.))
  return d[@dt = $max]/@v = '0'
]
+2

Source: https://habr.com/ru/post/1656788/


All Articles