Thanks for pointing out the XQuery 3.0 functions, Matthias!

fn:path is indeed supported, but I want a function that will do the same as 
fn:path, minus any of the values of "[1]".   Plus, since my current crop of 
documents don't have any associated namespaces, the processor that I'm using 
prepends "Q{}" before each path.  So, with the previous a/b/c example, I get 
the following:

/Q{}a[1]/Q{}b[1]/Q{}c[3]

But all I want is:

/a/b/c[3]

I'll try working on it again later.  Thanks again,

Mark



From: Matthias Brantner [mailto:[email protected]]
Sent: Tuesday, May 15, 2012 11:43 AM
To: David Lee
Cc: Custer, Mark; xquery-discuss
Subject: Re: [xquery-talk] Help with an XQuery function

Or fn:path (http://www.w3.org/TR/xpath-functions-30/#func-path) if your XQuery 
processor
already supports it.

Matthias

On May 15, 2012, at 8:39 AM, David Lee wrote:


Take a look at functx for examples of this

http://www.xqueryfunctions.com/xq/functx_path-to-node.html



----------------------------------------
David A. Lee
[email protected]<mailto:[email protected]>
http://www.xmlsh.org

From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Custer, Mark
Sent: Tuesday, May 15, 2012 11:29 AM
To: xquery-discuss
Subject: [xquery-talk] Help with an XQuery function

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]<mailto:[email protected]>
http://x-query.com/mailman/listinfo/talk

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

Reply via email to