eXist Xquery Examples
eXist Xquery Examples eXist Xquery Examples Copyright@ ♂猜猜♂. 2005. All rights reserved
以下ex:>代表输入表达式 re:>表示输出结果
--插入[使用exist的xquery数据库检索工具]说明--
[基础操作]
1 条件表达式
ex:>if(3 < 4) then 'yes!' else 'no!'
re:>yes!
ex:>
for$x in (-1.5, 0.4, 1.7)
return<amount>
{
if($x < 0)
thenconcat('(', -$x, ')')
else$x
}</amount>
re:>
<amount>(1.5)</amount>
<amount>0.4</amount>
<amount>1.7</amount>
说明:注意第一行的输出和其它两行有所不同
-$x对$x取反
2 定义本地变量
ex:>let$x := 5 let $y := 6 return 10*$x+$y
re:>56
3 使用',',其功能相当于数组
ex:>3,4,5
re:>3
4
5
扩展加入其它语句
ex:>3,4,5,let$t:=56 return $t
re:>3
4
5
56
4 在return中使用{} {expression} 执行出来表达式的结果
ex:>
let$i := 2 return
let$r := <em>Value </em>
return
<p>{$r} of 10*{$i} is{10*$i}.</p>
re:>
<p><em>Value </em> of 10*2is 20.</p>
5 简单循环语句
ex:>for$x in (1 to 3) return ($x,10+$x)
re:>1
11
2
12
3
13
ex:>for$speech in document('/db/tonybooks/Catalog.xml')//categoryName
return<br/>{$speech}
re:>略
ex:>
<html>{
let$book := document('mybook.xml')/book
for$ch in $book/chapter
return <h2>{$ch/title)</h2>
}</html>
re:>略
6 使用text(),返回内容
ex:>let$book:=document('examples.xml')/example-queries/query
return$book/code/text()
ex:>let$doc := document('examples.xml')//query/code/text()
return$doc
[函数的定义与调用] ex:>
declarenamespace jnb = 'http://ociweb.com/jnb';
declarevariable $jnb:pi as xs:decimal { 3.1416 };
declarefunction jnb:fib($i as xs:integer) as xs:integer {
if ($i = 0 or $i = 1)
then1
else jnb:fib($i - 1) + jnb:fib($i - 2)
};
jnb:fib(3),jnb:fib(4), jnb:fib(5), $jnb:pi
re:>
3
5
8
3.1416
[节点和节点类型 ]一下两个作用相同
ex:><greetingfrom='weiqi'>Hello, World!</greeting>
re:><greetingfrom='weiqi'>Hello, World!</greeting>
ex:>document {
element { 'greeting' } {
attribute { 'from ' } {'weiqi' },
'Hello, World!'
}
}
re:><greetingfrom ='weiqi'>Hello, World!</greeting>
[flwor表达式/数据库检索命令]标准类型for, let,where, order by, return
1for语句
ex:>
for$x in (1, 2, 3)
return<number>{ $x }</number>
re:>
<number>1</number>
<number>2</number>
<number>3</number>
2let语句
ex:>
let$a := (1, 2, 3)
return<numbers>{ $a }</numbers>
re:><numbers>12 3</numbers>
3where语句
ex:>
for$x in (1, 2, 3)
where$x >= 2
return <number>{ $x}</number>
re:>
<number>2</number>
<number>3</number>
4order by 语句
ex:>
for$x in (<greeting/>, <greeting from='weiqi'/>,<greeting from='brian'/>)
orderby $x/@from ascending empty least
return$x
re:>
<greeting/>
<greetingfrom='brian'/>
<greetingfrom='weiqi'/>
说明order by 有以下几个属性:
descending,ascending,empty greatest,empty least
分别测试一下
[数量操作]该操作非常有用该操作非常有用1some 和 every 例子
ex:>some$x in (1, 2, 3) satisfies $x >= 2
re:>true
ex:>every$x in (1, 2, 3) satisfies $x >= 2
re:>false
ex:>some$x in (1, 2, 3), $y in (3, 4, 5) satisfies $x = $y
re:>true
ex:>every$x in (1, 2, 3), $y in (3, 4, 5) satisfies $x = $y
re:>false
2count($a)
ex:>let$a:=collection('tonybooks')//category
returncount($a)
re:>返回40个1
ex:>for$a in collection('tonybooks')//category
returncount($a)
re:>返回40
区别以上例子
3 多重循环语句
ex:>
for$c in customers
for$o in orders
where$c.cust_id=$o.cust_id and $o.part_id='xx'
return$c.name
以上语句等价于sql语句
selectcustomers.name from customers, orders
wherecustomers.cust_id=orders.cust_id andorders.part_id='xx'
ex:>
for$book in bib.xml//book
let$title := $book/title
where$book/publisher = 'Addison-Wesley'
return
<bookInfo>{$title }</bookInfo>
[xpath例子] greetings.xml:
<?xmlversion='1.0' encoding='UTF-8'?>
<greetings>
<greetingfrom='weiqi'>Nihao!</greeting>
<greetingfrom='brian'>Hi!</greeting>
<greetingfrom='luc'>Bonjour!</greeting>
</greetings>
ex:>doc('greetings.xml')/greetings
re:><greetings>
<greetingfrom='weiqi'>Nihao!</greeting>
<greetingfrom='brian'>Hi!</greeting>
<greetingfrom='luc'>Bonjour!</greeting>
</greetings>
ex:>doc('greetings.xml')//greeting
re:><greetingfrom='weiqi'>Nihao!</greeting>
<greeting from='brian'>Hi!</greeting>
<greetingfrom='luc'>Bonjour!</greeting>
说明:注意以上两个例子的区别/greetings和//greetings,
前者表示从根节点开始逐级遍历,后者表示任何级的节点都遍历
ex:>doc('greetings.xml')//greeting[@from='weiqi']
re:><greetingfrom='weiqi'>Nihao!</greeting>
说明:@from 用来引用节点中的属性名称
引用节点名直接输入例如:greeting 或者 greetings就可以
ex:>doc('greetings.xml')//greeting/@from
re:>from='weiqi'from='brian' from='luc'
ex:>doc('greetings.xml')//greeting[1]
re:><greetingfrom='weiqi>Nihao!</greeting>
说明:使用'节点名'[n]表示输出第几个节点内容
待查:
let$a := 3,4 --错误
let$b := ($a, $a)
let$c := 99
let$d := ()
return(count($a), count($b), count($c), count($d))
evaluatesto (2, 4, 1, 0) because $b is the same as (3,4,3,4).
待查:
children(<p>Thisis <em>very</em> cool.</p>)
returnsthis sequence of 3 values:
'Thisis ', <em>very</em>, ' cool.'
================以下为未整理部分=============
[Modules]
You can put functions and variablesdeclarations into library modules. A library module is a file that starts witha module namespace declaration and contains declarations of functions,variables, etc., but does not contain an expression at the end. A main modulecontains an expression at the end. Both library modules and main modules canimport other library modules to access variables and functions declared in theimported module.
(: libfib.xq :)
module namespace jnb ='http://ociweb.com/jnb';
declare function jnb:fib($i as xs:integer)as xs:integer {
if($i <= 1)
then 1
else jnb:fib($i - 1) + jnb:fib($i - 2)
};
(: mainfib.xq: )
import module namespace jnb ='http://ociweb.com/jnb' at 'libfib.xq';
jnb:fib(6)
[weiqi@gao] $ xquery mainfib.xq # Saxon
13
Qexo supports compiled modules. A librarymodule is compiled to a Java class whose name is derived from the modulenamespace URI. A main module is compiled to a Java class whose name is derivedfrom the module file name.
[weiqi@gao] $ qexo -C libfib.xq # Compile to Java classcom.ociweb.jnb
(compiling libfib.xq)
[weiqi@gao] $ qexo --main -Cmainfib.xq # Compile to Java classmainfib
(compiling mainfib.xq)
[weiqi@gao] $ java mainfib
13
-------------------------------------------------------------------------
Type Specification
XQuery is a strongly typed programminglanguage. Like Java and C#, for example, it's a mix of static typing (typeconsistency checked at compile-time) and dynamic typing (run-time type tests).However, the types in XQuery are different from the classes familiar fromobject-oriented programming. Instead, it has types to match XQuery's datamodel, and it allows you to import types form XML Schema.
if ($child instance of element section)
then process-section($child)
else ( ) {--nothing--}
This invokes the process-section functionif the value of $child is an element whose tag name is section. XQuery has aconvenient typeswitch shorthand for matching a value against a number of types.The following converts a set of tag names to a different set.
define function convert($x) {
typeswitch ($x)
case element para return <p>{process-children($x)}</p>
case element emph return<em>{process-children($x)}</em>
default return process-children($x)
}
define function process-children($x) {
for$ch in children($x)
return convert($ch)
}
Hello,
I am a beginner in Xquery.
In my schema I have element which has IDREFattribute.I want to extract id value to which points and check whether thatvalue is equivalent to some value.
eg:
<AUTHOR EMAILID='psmith@sfsu.edu'>
<FNAME>smith</FNAME>
<MI>j</MI>
<LNAME>jen</LNAME>
</AUTHOR>
<PAPER PAPERID = 10 EMAILID ='sai@yahoo.com'>
<MAINATHR>SHARMA</MAINATHR>
<TITLE>ABC</TITLE>
</PAPER>
<PAPER PAPERID = 10 EMAILID ='sha@yahoo.com'>
<MAINATHR>SHARMA</MAINATHR>
<TITLE>ABC</TITLE>
</PAPER>
let $id := 'sai@yahoo.com',
for $paper in $doc/PAPER[@EMAILID = $id],
for $author in $doc/AUTHOR[@EMAILID = $id]
return
<resultPAPERID='{$paper/@PAPERID}'>
{$author/FNAME}
<result>
for $paper in $doc/PAPER,
for $author in $doc/AUTHOR
where $paper/@EMAILID = $auther/$EMAILID
return
<result PAPERID='{$paper/@PAPERID}'>
{$author/FNAME}
<result>
如上例程序,在xquery中引用节点和节点属性名称的时候稍有不同。
节点 paper 属性 @paper
I am completely new to XQuery..
I have the following which I would like torun on an .xml file...
for $book in bib.xml//book
let $title := $book/title
where $book/publisher = 'Addison-Wesley'
return
<bookInfo>
{ $title }
</bookInfo>
and the bib.xml file
<bib>
<book year='1994'>
<title>TCP/IPIllustrated</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price> 65.95</price>
</book>
<book year='1992'>
<title>Advanced Programming in theUnix environment</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>
<book year='2000'>
<title>Data on the Web</title>
<author><last>Abiteboul</last><first>Serge</first></author>
<author><last>Buneman</last><first>Peter</first></author>
<author><last>Suciu</last><first>Dan</first></author>
<publisher>Morgan KaufmannPublishers</publisher>
<price>39.95</price>
</book>
<book year='1999'>
<title>The Economics of Technologyand Content for Digital TV</title>
<editor>
<last>Gerbarg</last><first>Darcy</first>
<affiliation>CITI</affiliation>
</editor>
<publisher>Kluwer AcademicPublishers</publisher>
<price>129.95</price>
</book>
</bib>
-----------------------------------------------
最后附上一个好的教材:
http://monetdb.cwi.nl/XQuery/Demo/index.html
http://www.xml.com/pub/a/2005/03/02/xquery.html
http://www.xml.com/pub/a/2005/03/23/xquery-2.html
http://www.w3schools.com/xquery/default.asp
Copyright@ ♂猜猜♂. 2005. All rights reserved