Xquery教程

Xquery教程Xquery 教程给 XQuery 最好的解释就是 XQuery 和 XML 之间的联系就好比 SQL 和数据库之间的联系设计出 XQurey 的目的是为了查询 XML 中的数据可以把 XQurey 理解为 XMLQurey 查询 XQuery 参考你可以找到所有有关操作符号 内置函数以及数据类型的资料 XQuery 参考内容目录 XQuery 介绍介绍了 XQuery 的概念与它的用途 XQuery

Xquery教程
给XQuery最好的解释就是XQuery和XML之间的联系就好比SQL和数据库之间的联系
设计出XQurey的目的是为了查询XML中的数据
可以把XQurey理解为XML Qurey(查询)






XQuery 参考

你可以找到所有有关操作符号,内置函数以及数据类型的资料

XQuery 参考

内容目录

XQuery 介绍
w3pop.com / 2006-09-21

p.gif
XQuery 实例 n.gif


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

p.gifXQuery 介绍
XQuery FLWOR 表达式 n.gif


The XML Example Document
XML样文

 
                
 
                
 
                 
                 

Everyday Italian
Giada De Laurentiis
2005
30.00




















 
                 
                 
Harry Potter
J K. Rowling
2005

29.99
















 
                 
                 
XQuery Kick Start
James McGovern

Per Bothner
Kurt Cagle
James Linn
Vaidyanathan Nagarajan

2003
49.99































 
                 
                 
Learning XML

Erik T. Ray
2003
39.95


















How to Select Nodes From “books.xml”?
怎样从”books.xml”中选择节点

Functions
函数

doc("books.xml")

Path Expressions
路径表达式

doc("books.xml")/bookstore/book/title
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML







Predicates
谓语

doc("books.xml")/bookstore/book[price<30]
 
                  
                  
Harry Potter
J K. Rowling

2005
29.99
















XQuery FLWOR 表达式
w3pop.com / 2006-09-21

p.gifXQuery 实例
XQuery FLWOR + HTML n.gif

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
where $x/price>30
return $x/title




XQuery Kick Start

Learning XML




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







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)元素

Learning XML

XQuery Kick Start




XQuery FLWOR + HTML
w3pop.com / 2006-09-21

p.gifXQuery FLWOR 表达式
XQuery 术语 n.gif

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表达式中加入

    • 标签:


                         
    {
for $x in doc("books.xml")/bookstore/book/title order by $x return
  • {
  • $x} }
                            
    • Everyday Italian
    • Harry Potter
    • Learning XML
    • XQuery Kick Start
                            
      { for $x in doc("books.xml")/bookstore/book/title order by $x return
    • { data($x)}
    • }
                            
    • Everyday Italian
    • Harry Potter
    • Learning XML
    • XQuery Kick Start

    XQuery 术语
    w3pop.com / 2006-09-21

    p.gifXQuery FLWOR + HTML
    XQuery 语法 n.gif


    XQuery 术语

    Nodes 节点

                               
                               
                                                           
    Harry Potter
    J K. Rowling
    2005

    29.99
















                                                           (document node)                           
                                                           J K. Rowling                            (element node)
    lang="en" (attribute node)

    Atomic values
    “单元素(Atomic)”属性值

    J K. Rowling
    "en"

    Items 项目


    Relationship of Nodes
    节点间的关系

    Parent 父类

     
                                
                                
    Harry Potter

    J K. Rowling
    2005
    29.99
















    Children 子类

     
                                
                                
    Harry Potter
    J K. Rowling
    2005

    29.99
















    Siblings 同属类

     
                                
                                
    Harry Potter
    J K. Rowling

    2005
    29.99
















    Ancestors 祖类

     
                               
     
                                
                                

    Harry Potter
    J K. Rowling
    2005
    29.99




















    Descendants 下属类

     
                               
     
                                
                                
    Harry Potter
    J K. Rowling

    2005
    29.99
















    XQuery 语法
    w3pop.com / 2006-09-21

    p.gifXQuery 术语
    XQuery 添加元素和属性 n.gif


    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
    return if ($x/@category="CHILDREN")
    then {data($x/title)}
    else {data($x/title)}







    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()。

     
                                   
                                     Everyday Italian 
                                   
    Harry Potter
    Learning XML
    XQuery Kick Start








    XQuery Comparisons
    XQuery比较

    $bookstore//book/@q > 10
    The expression above returns true if any q attributes
    have values greater than 10.
    任意有q属性值大于10的,上述表达的返回结果就会是“true”(真)




    $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.
    如果表达返回的只是一个值大于10的q属性,上面的表达式才返回真,如果返回的q多于一个,就会出错。










    XQuery 添加元素和属性
    w3pop.com / 2006-09-21

    p.gifXQuery 语法
    XQuery 选择和过滤 n.gif

    The XML Example Document
    XML样文


    Adding Elements and Attributes to the Result
    在结果里添加元素和属性

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




    Everyday Italian

    Harry Potter
    Learning XML
    XQuery Kick Start










    Add HTML Elements and Text
    添加HTML元素和文本

     



    Bookstore

     
                                        

      {
      for $x in doc("books.xml")/bookstore/book
      order by $x/title
      return
    • {data($x/title)}. Category: {data($x/@category)}


    • }




















     



    Bookstore

     
                                        

    • Everyday Italian. Category: COOKING


    • Harry Potter. Category: CHILDREN

    • Learning XML. Category: WEB

    • XQuery Kick Start. Category: WEB


















    Add Attributes to HTML Elements
    添加属性给HTML元素

     







    Bookstore

     
                                        

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

    • }





















     



    Bookstore

     
                                        

    • Everyday Italian


    • Harry Potter

    • Learning XML

    • XQuery Kick Start


















    XQuery 选择和过滤
    w3pop.com / 2006-09-21

    p.gifXQuery 添加元素和属性
    XQuery 函数 n.gif

    Selecting and Filtering Elements
    选择和过滤元素

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







    • 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)
    return {$x}

     
                                          
                                            1 
                                          
    2
    3
    4

    5













    The at keyword can be used to count the iteration:
    关键字“at”可用于计算重复次数

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

     
                                          
                                            1. Everyday Italian 
                                          
    2. Harry Potter
    3. XQuery Kick Start

    4. Learning XML










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

     
                                          
                                            x=10 and y=100 
                                          
    x=10 and y=200
    x=20 and y=100
    x=20 and y=200







    The let Clause
    Let子句

    let $x := (1 to 5)
    return {$x}

     
                                          
                                            1 2 3 4 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
    order by $x/@category, $x/title
    return $x/title




    Harry Potter

    Everyday Italian
    Learning XML
    XQuery Kick Start










    The return Clause
    Return子句

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

    Everyday Italian

    Harry Potter
    XQuery Kick Start
    Learning XML










    XQuery 函数
    w3pop.com / 2006-09-21

    p.gifXQuery 选择和过滤
    XQuery 摘要 n.gif


    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
    函数调用实例

     
                                               
                                                 {uppercase($booktitle)} 
                                               
    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)
    AS returnDatatype
    {




    (: ...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(
    $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 :)
     
                                                 
                                                   {local:minPrice($book/price, $book/discount)} 
                                                 

    XQuery 摘要
    w3pop.com / 2006-09-21

    p.gifXQuery 函数
    XQuery 参考 n.gif

    XQuery Summary
    XQuery概要


    Now You Know XQuery, What's Next?
    明白了XQuery,接着学什么呢?

    XLink and XPointer

    XQuery 参考
    w3pop.com / 2006-09-21

    p.gifXQuery 摘要
    n.gif


    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

    (0)
    上一篇 2026年3月18日 上午10:41
    下一篇 2026年3月18日 上午10:41


    相关推荐

    • Claude Code 实际如何使用的一些经验分享(1)

      Claude Code 实际如何使用的一些经验分享(1)

      2026年3月16日
      2
    • SLAM算法调研「建议收藏」

      SLAM算法调研「建议收藏」作为一名机器人运行控制算法工程师,SLAM算法的调研已初步完成,特意分享。

      2022年6月16日
      49
    • MySQL数据库建立数据库和表(命令行方式)

      MySQL数据库建立数据库和表(命令行方式)最近在学数据库系统概论,以前建表都是直接用workbeach,但是作为一个计算机专业的学生,我觉得能敲的时候就少点,所以分享一个自己用命令创建数据库和表的过程,希望对一些人有点用!安装好数据库后,我们可以看到这些东西可以这么简单的认识,划红线的是通过命令行来操作数据库,划绿线的是操作数据库的图形化界面,这里我分享的是通过命令行来操作,以《数据库系统概论》第五版第三章为例创建一个我们平时…

      2022年7月19日
      18
    • 特殊多位数乘法口算算法

      特殊多位数乘法口算算法本文转自:我爱口算网,但是本人有一定更正,因此转载请注明出处一、关于9的数学口算技巧(两位数乘法)关于9的口诀:1×9=9  2×9=18  3×9=27    4×9=365×9=45  6×9=54  7×9=63    8×9=729×9=81上面的口诀小朋友们已经会了吗?小学

      2022年5月26日
      56
    • WDA问题混总

      WDA问题混总文章目录unabletoaccessDB:WebDriverAgentLibcodesign失败WebDriverAgentLibcodesignfailure.unabletoaccessDB:多个xcode线程在跑,杀掉xcode,杀掉xcodebuild进程,cleanWebDriverAgentLibcodesign失败环境:mac10.14.5xcode10.3报错:CommandCodesignfailedwithano

      2022年7月12日
      21
    • SpringBoot自定义starters

      SpringBoot自定义startersSpringBoot自定义starters1、简介2、如何自定义starter1、简介SpringBoot最强大的功能就是把我们常用的场景 抽象成一个个starter(场景启动器),我们通过引入springBoot为我们提供这些场景启动器,我们再进行少量的配置就能使用相应的功能。但是,SpringBoot不能包含所有的场景,经常需要我们自定义starter,来简化我们对springBoot的使用。2、如何自定义starter…

      2025年8月14日
      4

    发表回复

    您的邮箱地址不会被公开。 必填项已用 * 标注

    关注全栈程序员社区公众号