Subject:XQuery or XPath "between" function ... how to write ? Author:David Lee Date:17 Oct 2006 05:27 PM
I'm trying to write an XQuery function that returns a subset of an item list "between" two items, identified by a condition ...
The goal is to take something like this
let $list := ("a","bunch","of","<tag>","words","with","markup","</tag>","in","a","list")
and call some function or xpath expression given "tag" or even better yet a regex pair like "<[a-z]+>" , "</[a-z]+>" and have it return
("<tag>","words","with","markup", "</tag>")
or even better
("words","with","markup")
The ultimate goal is to take a list of tokenized strings and dynamically an element based on text markup ... but even having something primative like
fn:between( $n as item()* , $first as item() , $last as item() ) as item()*
Suggestions greatly welcome !
I have no problems locating the first and last nodes, I'm just getting stuck generating the list of nodes between them (ideally exclusive of them)
Subject:XQuery or XPath Author:David Lee Date:17 Oct 2006 07:24 PM
I firgured this out finally ... took a while (XQuery is FUN ... but alas not so easy until you know how ... like everything else :)
declare function local:between( $list as node()* , $first as node() , $last as node() ) as node()*
{
$list[ . >> $first ][ . << $last ]
};
I also had a similar more complex one that worked too:
declare function local:between( $list as node()* , $first as node() , $last as node() ) as node()*
{
for $n in $list
where (($n >> $first) and ($n << $last ))
return $n/text()
};
Any comments on these ? They "appear to work" ... but are they correct ? Any reason to pick one over the other ? My guess is the first one is more efficient ... but who knows ...
Subject:XQuery or XPath Author:David Lee Date:19 Oct 2006 11:19 AM
Thanks, this helps a lot.
I was struggling trying to create a function that created a list which contains sub-lists which is not really possible (without constructing dummy node variabls) ... like ((1,2),(3,4)) is really (1,2,3,4)
but I hadnt thought of using "mod" ... thats useful !