---
title: "XQuery Expression learning"
author: "lvjun106"
date: "2012-10-15T23:14:51Z"
tags: []
categories: ["编程语言"]
slug: "xquery-expression-learning"
draft: false
lastmod: "2012-10-15T23:14:51Z"
id: 1698880
url: "https://lvjun106.iteye.com/blog/1698880"
---

open the file: doc("xxx.xml")

 

doc("xxx.xml")/bookstore/book\[conditon clause]

 

for, where, order, return

 

doc("xxx.xml")/bookstore/book\[price&gt;20]/title

=

for $x in doc("xxx.xml")/bookstore/book

where $x/price&gt;20

order by $x/title

return $x/title

 

integrate with html tags

&lt;ul&gt;

for $x in doc("xxx.xml")/bookstore/book/title

oder by $x

return &lt;li&gt;($x)&lt;/li&gt;

&lt;/ul&gt; 

 

get the data directly use the data() function

ex:

&lt;ul&gt;

for $x in doc("xxx.xml")/bookstore/book/title

return &lt;li&gt;(data|($x))&lt;/li&gt;

&lt;/ul&gt;

 

basic syntax rules

comment: (:  I am comment :)

"if-then-else": 

for $x in doc("xxx.xml")/bookstore/book

return if ($x/@attribute="hey")

then &lt;child&gt;(data($x/title))&lt;/child&gt;

else &lt;adult&gt;(data($x/title))&lt;adult&gt;

comparisons:

one group: =, !=, &lt;, &lt;=, &gt;, &gt;=

another group: eq, ne, lt, le, gt, ge

what's the difference? see the follow two examples

$bookstore//book/@q &gt; 20   --&gt; if any items fulfill the condition,  then return true

$bookstore//book/@q lt 20   --&gt; only one item fulfill the condition, then return true

 

use function

default functions: upper-case, substring, data, and so on...

custom function:

declare function prefix:function\_name($parameter AS datatype)

AS returnDataType{

  ...

}