|
next
|
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)
|
next
|
Subject: XQuery or XPath Author: Ivan Pedruzzi Date: 18 Oct 2006 10:47 PM
|
Hi David,
Is your input well formed markup or a string?
Are you assuming to extract only one sub-region?
Here a simple solution that builds a list of begin/end pairs
declare variable $list := ("a","bunch","of","<tag>","words","with","markup","</tag>","in","a","list");
declare variable $indexes :=
for $item at $pos in $list
return
if(contains($item , "<tag>")) then $pos
else if(contains($item , "</tag>")) then $pos
else ();
for $i at $pos in $indexes[position() mod 2 = 1]
return subsequence($list, $i, $indexes[$pos + 1] - $i + 1)
Hope this helps
Ivan Pedruzzi
Stylus Studio Team
|
|
|
|