Search code examples
loopsxqueryparent

Is it possible to access the parent node inside the loop?


I have a loop like this:

for $a in local:distinct-nodes($x/input/book[@stat = 'ok'])
let $b:=string($a/../input/@docid)

This returns an empty string($b is empty). So, I am wondering how I can access the parent node in this situation. I do not really want to access it from root (//xxxx) cause the XML file is quite large.


Solution

  • I have a loop like this:

     for $a in local:distinct-nodes($x/input/book[@stat = 'ok']) 
     let $b:=string($a/../input/@docid) 
    

    This returns an empty string($b is empty).

    Yes, this is what should be produced.

    All $a nodes are book elements.

    $a/..  
    

    are input elements

    $a/../input
    

    should be input/input elements, but most probably input elements dont have input children.

    What you want is:

    $a/../@docid