Xquery教程
给XQuery最好的解释就是XQuery和XML之间的联系就好比SQL和数据库之间的联系
设计出XQurey的目的是为了查询XML中的数据
可以把XQurey理解为XML Qurey(查询)
XQuery 参考
你可以找到所有有关操作符号,内置函数以及数据类型的资料
XQuery 参考
内容目录
What You Should Already Know
你应该已经掌握的
- HTML / XHTML
- XML / XML Namespaces
- XPath
What is XQuery?
什么是XQuery?
- XQuery is the language for querying XML data
• XQuery是查询XML数据的语言 - XQuery for XML is like SQL for databases
• XQuery相当于XML的SQL数据库 - XQuery is built on XPath expressions
• XQuery 是建立在XPath表达式之上的 - XQuery is defined by the W3C
• XQuery是由W3C 定义的 - XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.)
• 所有重要的数据库引擎(IBM, Oracle, Microsoft, 等等.)都支持XQuery - XQuery will become a W3C standard – and developers can be sure that the code will work among different products
• XQuery将会成为W3C的标准——并且开发者都相信代码将可以在不同的产品之间运行
XQuery is About Querying XML
XQuery就是有关查询XML的
XQuery and XPath
XQuery – Examples of Use
XQuery使用例子
- Extract information to use in a Web Service
• 摘录在网络服务中使用的信息 - Generate summary reports
• 产生综合报告。 - Transform XML data to XHTML
• 把XML数据转换成XHTML形式 - Search Web documents for relevant information
• 在网络文件中搜索相关信息
XQuery is Not (Yet) a Web Standard
XQuery还不是网络标准
XQuery 实例
w3pop.com / 2006-09-21
XQuery 介绍
XQuery FLWOR 表达式
The XML Example Document
XML样文
|
How to Select Nodes From “books.xml”?
怎样从”books.xml”中选择节点
Functions
函数
doc("books.xml")
|
Path Expressions
路径表达式
doc("books.xml")/bookstore/book/title
|
|
Predicates
谓语
doc("books.xml")/bookstore/book[price<30]
|
|
XQuery FLWOR 表达式
w3pop.com / 2006-09-21
XQuery 实例
XQuery FLWOR + HTML
The XML Example Document
XML样文
How to Select Nodes From “books.xml” With FLWOR
怎样用FLWOR表达式从”books.xml”文件中选择节点
doc("books.xml")/bookstore/book[price>30]/title
|
for $x in doc("books.xml")/bookstore/book
|
|
for $x in doc("books.xml")/bookstore/book
|
The for clause selects all book elements under the bookstore element into a variable called $x.
For子句把bookstore元素里的所有book元素送到名为$x.的变量里
The where clause selects only book elements with a price element with a value greater than 30.
Where子句只选择price元素值高于30的book元素
The order by clause defines the sort-order. Will be sort by the title element.
order by子句定义了“分类命令”。会被标题元素分类
The return clause specifies what should be returned. Here it returns the title elements.
Return子句详细定义了哪些数据该被返回。这里返回的是标题(title)元素
|
XQuery FLWOR + HTML
w3pop.com / 2006-09-21
XQuery FLWOR 表达式
XQuery 术语
The XML Example Document
XML样文
Present the Result In an HTML List
在HTML列表中显示结果
for $x in doc("books.xml")/bookstore/book/title order by $x return $x
|
Now we want to list all the book-titles in our bookstore in an HTML list. We add
-
and
- tags to the FLWOR expression:
现在我们想要在 HTML列表中列出书店里的所有book-titles元素。我们在FLWOR表达式中加入-
和
- 标签:
|
|
|
|
XQuery 术语
w3pop.com / 2006-09-21
XQuery FLWOR + HTML
XQuery 语法
XQuery 术语
Nodes 节点
|
lang="en" (attribute node) |
Atomic values
“单元素(Atomic)”属性值
J K. Rowling "en" |
Items 项目
Relationship of Nodes
节点间的关系
Parent 父类
|
Children 子类
|
Siblings 同属类
|
Ancestors 祖类
|
Descendants 下属类
|
XQuery 语法
w3pop.com / 2006-09-21
XQuery 术语
XQuery 添加元素和属性
XQuery Basic Syntax Rules
XQuery的基本语法规则
- XQuery is case-sensitive
• XQuery区分大小写 - XQuery elements, attributes, and variables must be valid XML names
• 它的元素,属性,变量必须是有效的XML名称 - An XQuery string value can be in single or double quotes
• 一个XQuery字符串值可以写在单引号或双引号里 - An XQuery variable is defined with a $ followed by a name, e.g. $bookstore
• 一个XQuery变量定义是在“$”的符号后面跟上名称等,例如:$bookstore - XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)
• XQuery注释用“(: ”和“ :)”进行分界,例如(: XQuery Comment :)
XQuery Conditional Expressions
XQuery的条件表达式
for $x in doc("books.xml")/bookstore/book
|
Notes on the “if-then-else” syntax: parentheses around the if expression are required. else is required, but it can be just else ().
在使用“if-then-else”条件语句时应注意的语法点:if表达式允许有圆括号,另外一点就是,使用了“if”就必须用到“else”关键词,可以是else()。
|
XQuery Comparisons
XQuery比较
$bookstore//book/@q > 10
The expression above returns true if any q attributes $bookstore//book/@q gt 10
The expression above returns true if there is only one |
XQuery 添加元素和属性
w3pop.com / 2006-09-21
XQuery 语法
XQuery 选择和过滤
The XML Example Document
XML样文
Adding Elements and Attributes to the Result
在结果里添加元素和属性
for $x in doc("books.xml")/bookstore/book/title
|
|
Add HTML Elements and Text
添加HTML元素和文本
{ for $x in doc("books.xml")/bookstore/book order by $x/title return } |
|
Add Attributes to HTML Elements
添加属性给HTML元素
{ for $x in doc("books.xml")/bookstore/book order by $x/title return } |
|
XQuery 选择和过滤
w3pop.com / 2006-09-21
XQuery 添加元素和属性
XQuery 函数
Selecting and Filtering Elements
选择和过滤元素
for $x in doc("books.xml")/bookstore/book
|
- for – (optional) binds a variable to each item returned by the in expression
• for -(可选的)给从表达式内返回每一个项绑定一个变量。 - let – (optional)
let -(可选的) - where – (optional) specifies a criteria
where -(可选的)指定了一个标准 - order by – (optional) specifies the sort-order of the result
• order by—(可选的)指定了结果的排列次序 - return – specifies what to return in the result
• return-指定了在结果中返回的内容
The for Clause
For子句
To loop a specific number of times in a for clause, you may use the to keyword:
在for子句中对一个指定的数字进行多次循环,你可以使用关键词“to”
for $x in (1 to 5) |
|
The at keyword can be used to count the iteration:
关键字“at”可用于计算重复次数
for $x at $i in doc("books.xml")/bookstore/book/title
|
|
for $x in (10,20), $y in (100,200) |
|
The let Clause
Let子句
let $x := (1 to 5) |
|
The where Clause
Where子句
where $x/price>30 and $x/price<100 |
The order by Clause
order by子句
for $x in doc("books.xml")/bookstore/book
|
|
The return Clause
Return子句
for $x in doc("books.xml")/bookstore/book
|
|
XQuery 函数
w3pop.com / 2006-09-21
XQuery 选择和过滤
XQuery 摘要
XQuery Functions
XQuery函数
XQuery Built-in Functions
XQuery内置函数
Tip: Functions are often called with the fn: prefix, such as fn:string(). However, since fn: is the default prefix of the namespace, the function names do not need to be prefixed when called.
提示:函数常常以fn:前缀名调用,例如fn:string();然而,因为fn:是名称空间(namespaces)的默认前缀,所以这个函数的名称并不需要以前缀名调用。
Examples of Function Calls
函数调用实例
|
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']
|
let $name := (substring($booktitle,1,4)) |
XQuery User-Defined Functions
XQuery自定义函数
Syntax
语法
declare function prefix:function_name($parameter AS datatype) (: ...function code here... :) }; |
- Use the declare function keyword
• 用到“declare function”(函数声明)关键词 - The name of the function must be prefixed
• 函数名称要前缀化 - The data type of the parameters are mostly the same as the data types defined in XML Schema
• 参数的数据类型和XML Schema的数据类型要基本一致 - The body of the function must be surrounded by curly braces
• 函数的主体部分要写在圆括号里
Example of a User-defined Function Declared in the Query
在查询段里声明的自定义函数举例
declare function local:minPrice( (: Below is an example of how to call the function above :)
|
XQuery 摘要
w3pop.com / 2006-09-21
XQuery 函数
XQuery 参考
XQuery Summary
XQuery概要
Now You Know XQuery, What's Next?
明白了XQuery,接着学什么呢?
XLink and XPointer
XQuery Functions
XQuery函数
XQuery Data Types
XQuery数据类型
转载于:https://www.cnblogs.com/xujiaci/archive/2007/08/30/875927.html
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216950.html原文链接:https://javaforall.net
