Hi all,

I'm quite new to XQuery, and I was wondering if I someone could give me 
feedback on a simple function that I wanted to write.  Once finished, the 
function should provide the XPATH to an individual node, complete with each 
node's position in the path (but only if there is more than one preceding 
sibling with an equivalent node name).   So, if you had an XML document like:

<a>
        <b>
                <c/>
                <c/>
                <c>test the function here</c>
        </b>
</a>

...and if the function was called while processing that last c node, it should 
print out:

/a/b/c[3]

So far, I have the following XQuery:

declare namespace local = "http://local";;
declare function local:xpath
  ($nodes as node()*) as xs:string* {

for $node in $nodes/ancestor-or-self::*
let $siblings := count($node/preceding-sibling::*[name()=name($node)])
        for $number in $siblings
        let $position := $number
return  if($position gt 0)
then concat('/', name($node), '[', $position + 1, ']')
                else concat('/', name($node))
 };

This works, but I was wondering if it could be written better (and I imagine 
that someone already has written a concise XQuery function to do this).  Also, 
I'm not entirely sure why, but when I run the above function, I get the 
following result:

/a /b /c[3]

...where there is a single space present after each returned value, aside from 
the last one.

Any advice or suggestions are welcome.  Thanks,

Mark



_______________________________________________
[email protected]
http://x-query.com/mailman/listinfo/talk

Reply via email to