XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
David LeeSubject: 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)


Postnext
David LeeSubject: 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 ...

Postnext
Ivan PedruzziSubject: 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

Posttop
David LeeSubject: 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 !

Thank you

-David

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.