XQuery

What is XQuery?

  • XQuery is the language for querying XML data
  • XQuery for XML is like SQL for databases
  • XQuery is built on XPath expressions
  • XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.)

Functions

doc("books.xml")
doc("books.xml")/bookstore/book/title
doc("books.xml")/bookstore/book[price
< 30 ]
doc("books.xml")/bookstore/book[price
> 30]/title

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

< ul >
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return 
< li > {$x} </ li >
}
</ ul >

< ul >
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return 
< li > {data($x)} </ li >
}
</ ul >

XQuery Basic Syntax Rules

Some basic syntax rules:
  • XQuery is case-sensitive
  • XQuery elements, attributes, and variables must be valid XML names
  • An XQuery string value can be in single or double quotes
  • An XQuery variable is defined with a $ followed by a name, e.g. $bookstore
  • XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)

XQuery Basic Syntax Rules

for $x in doc("books.xml")/bookstore/book
return    if ($x/@category="CHILDREN")
    then 
< child > {data($x/title)} </ child >
    else 
< adult > {data($x/title)} </ adult >

In XQuery there are two ways of comparing values.

1. General comparisons: =, !=, <, <=, >, >=
2. Value comparisons: eq, ne, lt, le, gt, ge
$bookstore//book/@q > 10
The expression above returns true if any q attributes have values greater than 10.

$bookstore//book/@q gt 10
The expression above returns true if there is only one q attribute returned by the expression, and its value is greater than 10. If more than one q is returned, an error occurs.

(: Adding Elements and Attributes to the Result :)
< html >
< body >
< h1 > Bookstore </ h1 >
< ul >
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return 
< li  class ="{data($x/@category)}" > {data($x/title)} </ li >
}
</ ul >
</ body >
</ html >

(: XQuery Selecting and Filtering :)
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

for $x in (1 to 5)
return 
< test > {$x} </ test >

for $x at $i in doc("books.xml")/bookstore/book/title
return 
< book > {$i}. {data($x)} </ book >

for $x in (10,20), $y in (100,200)
return 
< test > x={$x} and y={$y} </ test >

let $x := (1 to 5)
return 
< test > {$x} </ test >

(:XQuery Built-in Functions :)
< name > {uppercase($booktitle)} </ name >
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']

(: XQuery User-Defined Functions
   Syntax :)

declare function prefix:function_name($parameter AS datatype)
  AS returnDatatype
{
(: function code here :)
};

declare function local:minPrice(
  $price as xs:decimal?,
  $discount as xs:decimal?)
  AS xs:decimal?
{
let $disc := ($price * $discount) div 100
return ($price - $disc)
};
(: Below is an example of how to call the function above :)
< minPrice > {local:minPrice($book/price, $book/discount)} </ minPrice >
let $name := (substring($booktitle,1,4))

* Reference to W3C Schools
posted on 2007-11-19 16:20 Sady 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/sady/archive/2007/11/19/964419.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值