Hello all,

I've been looking for an authoritative answer to this, but have been unable
to find one.

I'm trying to reliably get the nearest ancestor of a node. ie with this
test data:

let $test :=
element test {
  attribute id { 4 },
  element test {
    attribute id { 3 },
    element test {
      attribute id { 2 },
      element test {
        attribute id { 1 },
        element child { }
      }
    }
  }
}

i want an expression that will return node test/@id=1 from child.

Stackoverflow says that this:

$test//child/ancestor-or-self::test[ last() ]

Should work 
(here<http://stackoverflow.com/questions/6554488/how-to-get-the-nearest-ancestor-or-child-of-an-ancestor-with-xpath>),
but since I couldn't find any indication to reversed axes being ordered in
the w3c spec for reverse axes in xpath 2.0
(here<http://www.w3.org/TR/xpath20/#doc-xpath-ReverseAxis>),
I suspected that this would be implementation dependent, and sure enough,
in BaseX, that expression returns

<test id="4">
  <test id="3">
    <test id="2">
      <test id="1">
        <child/>
      </test>
    </test>
  </test>
</test>

while this

$test//child/ancestor-or-self::test[ 1 ]

​correctly returns

<test id="1">
  <child/>
</test>​

Is there an authoritative way of getting the nearest ancestor of a given
node? Note that in my actual use case, names are the same, and desired ids
are unknown.

Thanks!

jta

-- 
entia non sunt multiplicanda praeter necessitatem

Reply via email to