Search code examples
xpathxqueryxquery-3.0

XQuery and XPath - testing for child/attribute based on current attribute


I have numerous xml files structured

<seg type="dep_event" xml:id="MS609-0000-01">
  <date type="event_date" when="1245"/>
</seg>

Where <date type="event_date" when="1245"> is optional.

Now in Xpath/Xquery 3.1, I need test for the existence of date@when, based on the current context node seg/@xml:id.

Imagine this situation in Xquery 3.1:

let $doc := doc(somedocument.xml)

for $xmlidattr in $doc//tei:seg[@type="dep_event"]/@xml:id

Now, I need to test date/@when using $xmlidattr

Many thanks in advance.

EDIT: restructured question to be more clear about the idea of context node in XQuery


Solution

  • The parent of the id is the seg, its child is the date:

    ../date[@type="event_date"][@when="1245"]
    

    That was the answer to the original question. The question has changed and the new answer is

    $xmlidattr/../date[@type="event_date"][@when="1245"]
    

    However, I would normally start from the containing element rather than from the attribute:

    for $seg in $doc//tei:seg[@type="dep_event"],
        $xmlidattr in $seg/@xml:id
    return f($seg/date[@type="event_date"][@when="1245"])