以文本方式查看主题 - 中文XML论坛 - 专业的XML技术讨论区 (http://bbs.xml.org.cn/index.asp) -- 『 XQuery/XLink/XPointer/ 』 (http://bbs.xml.org.cn/list.asp?boardid=14) ---- XQuery从头学 (http://bbs.xml.org.cn/dispbbs.asp?boardid=14&rootid=&id=45336) |
-- 作者:xml-linguist -- 发布时间:4/12/2007 3:33:00 PM -- XQuery从头学 Let's try to learn some basic XQuery syntax by looking at an example. 从实例直接开始。 We will use the following XML document in the examples below. "books.xml": <?xml version="1.0" encoding="ISO-8859-1"?> How to Select Nodes From "books.xml"? Functions The doc() function is used to open the "books.xml" file: Path Expressions The following path expression is used to select all the title elements in the "books.xml" file: (/bookstore selects the bookstore element, /book selects all the book elements under the bookstore element, and /title selects all the title elements under each book element) The XQuery above will extract the following: <title lang="en">Everyday Italian</title> Predicates The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30: The XQuery above will extract the following: <book category="CHILDREN">
[此贴子已经被作者于2007-4-14 18:18:20编辑过]
|
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:38:00 AM -- XQuery FLWOR Expressions XQuery FLWOR表达式 The XML Example Document <?xml version="1.0" encoding="ISO-8859-1" ?> Look at the following path expression: doc("books.xml")/bookstore/book[price>30]/title The expression above will select all the title elements under the book elements that are under the bookstore element that have a price element with a value that is higher than 30. The following FLWOR expression will select exactly the same as the path expression above: for $x in doc("books.xml")/bookstore/book The result will be: <title lang="en">XQuery Kick Start</title> With FLWOR you can sort the result: for $x in doc("books.xml")/bookstore/book FLWOR is an acronym for "For, Let, Where, Order by, Return". The for clause selects all book elements under the bookstore element into a variable called $x. The where clause selects only book elements with a price element with a value greater than 30. The order by clause defines the sort-order. Will be sort by the title element. The return clause specifies what should be returned. Here it returns the title elements. The result of the XQuery expression above will be: <title lang="en">Learning XML</title> (注:由于使用了order by指令,title元素值的顺序按字母顺序排列了。)
|
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:40:00 AM -- XQuery FLWOR + HTML FLWOR表达式和HTML的综合应用 ------------------------------------------------------------------------------- The XML Example Document <?xml version="1.0" encoding="ISO-8859-1" ?> Present the Result In an HTML List Look at the following XQuery FLWOR expression: The expression above will select all the title elements under the book elements that are under the bookstore element, and return the title elements in alphabetical order. Now we want to list all the book-titles in our bookstore in an HTML list. We add <ul> and <li> tags to the FLWOR expression: <ul> The result of the above will be: <ul> Now we want to eliminate the title element, and show only the data inside the title element: <ul> The result will be (an HTML list): <ul> [此贴子已经被作者于2007-4-13 10:10:37编辑过]
|
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:40:00 AM -- XQuery Terms XQuery表达方式 In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XQuery Terminology In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node). Look at the following XML document: <?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book> Example of nodes in the XML document above: <bookstore> (document node) Atomic values Example of atomic values: Items Relationship of Nodes Parent In the following example; the book element is the parent of the title, author, year, and price: Children In the following example; the title, author, year, and price elements are all children of the book element: <book> Siblings In the following example; the title, author, year, and price elements are all siblings: <book> Ancestors In the following example; the ancestors of the title element are the book element and the bookstore element: <bookstore><book> Descendants In the following example; descendants of the bookstore element are the book, title, author, year, and price elements: <bookstore><book>
[此贴子已经被作者于2007-4-15 7:59:59编辑过]
|
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:40:00 AM -- XQuery Syntax XQuery语法 -------------------------------------------------------------------------------- XQuery is case-sensitive and XQuery elements, attributes, and variables must be valid XML names. XQuery Basic Syntax Rules XQuery is case-sensitive XQuery Conditional Expressions Look at the following example: 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 (). The result of the example above will be: <adult>Everyday Italian</adult> XQuery Comparisons In XQuery there are two ways of comparing values. 1. General comparisons: =, !=, <, <=, >, >= 2. Value comparisons: eq, ne, lt, le, gt, ge The difference between the two comparison methods are shown below. Look at the following XQuery expressions: $bookstore//book/@q>10 $bookstore//book/@q gt 10 (注:最后两段的翻译有点照猫画虎了,因为我自己对原文吃不透了。请各位把正确的理解和表达方式跟贴发出,我再更改。多谢了!)
[此贴子已经被作者于2007-4-14 9:47:52编辑过]
|
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:40:00 AM -- XQuery Adding Elements and Attributes XQuery添加元素和属性 -------------------------------------------------------------------------------- The XML Example Document We will use the "books.xml" document (same XML file as in the previous chapters). 还是以前面几节出现的"books.xml"文件为示例。 -------------------------------------------------------------------------------- Adding Elements and Attributes to the Result As we have seen in a previous chapter, we may include elements and attributes from the input document ("books.xml) in the result: for $x in doc("books.xml")/bookstore/book/title The XQuery expression above will include both the title element and the lang attribute in the result, like this: <title lang="en">Everyday Italian</title> The XQuery expression above returns the title elements the exact same way as they are described in the input document. We now want to add our own elements and attributes to the result! Add HTML Elements and Text Now, we want to add some HTML elements to the result. We will put the result in an HTML list - together with some text: <html> The XQuery expression above will generate the following result: <html> Add Attributes to HTML Elements <html> The XQuery expression above will generate the following result: <html> [此贴子已经被作者于2007-4-14 11:16:59编辑过]
|
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:40:00 AM -- XQuery Selecting and Filtering XQuery选择和过滤 -------------------------------------------------------------------------------- The XML Example Document XML实例文档 We will use the "books.xml" document in the examples below (same XML file as in the previous chapters). 我们使用这个"books.xml"文档(和上面的几节中所使用的XML文件相同)。 -------------------------------------------------------------------------------- Selecting and Filtering Elements 选择和过滤元素 As we have seen in the previous chapters, we are selecting and filtering elements with either a Path expression or with a FLWOR expression. Look at the following FLWOR expression: for $x in doc("books.xml")/bookstore/book for - (optional) binds a variable to each item returned by the in expression for(可选) 为每个由in表达式返回的项绑定一个变量 To loop a specific number of times in a for clause, you may use the to keyword: for $x in (1 to 5) Result: <test>1</test> The at keyword can be used to count the iteration: for $x at $i in doc("books.xml")/bookstore/book/title Result: <book>1. Everyday Italian</book> It is also allowed with more than one in expression in the for clause. Use comma to separate each in expression: for $x in (10,20), $y in (100,200) Result: <test>x=10 and y=100</test> The let Clause let $x := (1 to 5) Result: <test>1 2 3 4 5</test> The where Clause where $x/price>30 and $x/price<100 The order by Clause for $x in doc("books.xml")/bookstore/book Result: <title lang="en">Harry Potter</title> The return Clause for $x in doc("books.xml")/bookstore/book Result: <title lang="en">Everyday Italian</title>
[此贴子已经被作者于2007-4-16 10:58:11编辑过]
|
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:41:00 AM -- XQuery Functions -------------------------------------------------------------------------------- XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library. XQuery Functions XQuery Built-in Functions The default prefix for the function namespace is fn:. 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. The reference of all the built-in XQuery 1.0 functions is located in our XPath tutorial. Examples of Function Calls Example 1: In an element <name>{uppercase($booktitle)}</name> Example 2: In the predicate of a path expression doc("books.xml")/bookstore/book[substring(title,1,5)='Harry'] Example 3: In a let clause let $name := (substring($booktitle,1,4)) XQuery User-Defined Functions User-defined functions can be defined in the query or in a separate library. Syntax Notes on user-defined functions: Use the declare function keyword XQuery 函数 http://www.w3.org/2005/02/xpath-functions 函数命名空间的默认前缀是fn:。 提示:函数经常被通过fn:前缀进行调用,例如fn:string()。不过,由于fn:是命名空间的默认前缀,所以函数名称不必在被调用时使用前缀。 您可以在我们的XPath教程中找到完整的《内建XQuery函数参考手册》。 例1:在元素中 可在查询中或独立的库中定义用户自定义函数。 语法 (: ...函数代码... :) };关于用户自定义函数的注释: (: Below is an example of how to call the function above :) <minPrice>{local:minPrice($book/price, $book/discount)}</minPrice> (注:这一块学了两遍,有点搞不清,直接抄了别人的翻译资料过来。再学学看!)
[此贴子已经被作者于2007-4-18 11:05:53编辑过]
|
-- 作者:xml-linguist -- 发布时间:4/13/2007 3:04:00 PM -- You Have Learned XQuery, Now What? 已经学习了XQuery,下一步干什么? -------------------------------------------------------------------------------- XQuery Summary This tutorial has taught you how to query XML data. You have learned that XQuery was designed to query anything that can appear as XML, including databases. You have also learned how to query the XML data with FLWOR expressions, and how to construct XHTML output from the collected data. For more information on XQuery, please look at our XQuery Reference. -------------------------------------------------------------------------------- The next step is to learn about XLink and XPointer. Linking in XML is divided into two parts: XLink and XPointer. XLink and XPointer define a standard way of creating hyperlinks in XML documents. If you want to learn more about XLink and XPointer, please visit XLink and XPointer tutorial. [此贴子已经被作者于2007-4-18 11:19:05编辑过]
|
-- 作者:jx -- 发布时间:4/13/2007 8:21:00 PM -- 欢迎,请继续。 |
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:35:00 PM -- 谢谢!我会坚持下去的。 我自己要学一遍,顺便贴在这里,希望对初学的人有帮助。 |
-- 作者:xml-linguist -- 发布时间:4/13/2007 9:37:00 PM -- 有些术语我拿不准,希望大家指出来。 |
-- 作者:cuiyaxin -- 发布时间:4/13/2007 10:53:00 PM -- 你贴的是什么呢 如果是适合初学者的 请给我一份巴 cyx2121921@sina.com |
-- 作者:xml-linguist -- 发布时间:4/14/2007 9:52:00 AM --
对不起cuiyaxin!我是边学边做,直接在网络上做,我自己都没有备份。你直接看就行了,如果需要,麻烦你动手copy一下吧! |
-- 作者:jx -- 发布时间:4/15/2007 2:20:00 PM -- 支持! 鼓励你! |
-- 作者:xml-linguist -- 发布时间:4/16/2007 11:35:00 AM -- 谢谢支持! 学完以后,应该实践了,估计难度更大一点。 只有实践,才能真正学会! |
-- 作者:jx -- 发布时间:4/19/2007 7:26:00 AM -- 继续吧,很好啊咯! |
-- 作者:jingle_even -- 发布时间:4/30/2007 3:05:00 PM -- 楼主辛苦了!UP UP! |
-- 作者:smilemagi -- 发布时间:5/9/2007 8:06:00 PM -- http://www.w3schools.com/xquery/default.asp lz应该是在这里看的,这个网站是个很好的初学者网站,而且包含大多编程语言的初学教程^^ |
-- 作者:jiafan -- 发布时间:5/21/2007 1:16:00 PM -- 好人啊 辛苦 |
-- 作者:jx -- 发布时间:6/3/2007 12:52:00 PM -- 继续写出来。 |
-- 作者:mycatboys -- 发布时间:6/26/2007 8:20:00 PM -- 支持
------------------------------------------------------------------------------------------------------------------ |
-- 作者:lish -- 发布时间:9/12/2007 4:08:00 PM -- thans very much |
-- 作者:jx -- 发布时间:9/23/2007 6:25:00 PM -- 请继续。 |
-- 作者:龙藤 -- 发布时间:10/16/2007 3:39:00 PM -- 很好,谢谢 |
-- 作者:feathen1983 -- 发布时间:10/16/2007 8:24:00 PM -- xquery应该写在哪? 请问XQUERY的平台是什么?了解了语法和其他相关知识,但还是不知道该把语句写在哪才能显示出查询结果 |
-- 作者:viva156 -- 发布时间:12/19/2007 2:14:00 AM -- 一个问题 我不知道怎么执行xquery。。。 比较郁闷 表达式基本上可以看懂 我用xmlbean 执行老是说没有找到query引擎 |
-- 作者:kevin86713 -- 发布时间:1/15/2008 10:46:00 PM -- 太好了~~ 楼主加油~! |
-- 作者:xml-linguist -- 发布时间:7/28/2008 9:07:00 AM -- 其实,我根本不懂XQuery是干什么的。 在我做资料的过程中,有些构思很难一步到位:任何一个方案都要花去我大量的时间,而无论如何最终的结果还是无法令人满意。 学习XQuery纯粹是未来了却心愿:既然学xml,顺便把该知道的都学学吧!我就是这样才在一年前敷衍了事的看了一边XQuery,但是实际应用才是最近的事。 现在回头一看,我一前做的资料,无论多么不理想,不需再重新升级,只用XQuery把所需的数据抽取出来就好了。我现在只能做这一步。 |
-- 作者:venee_lee -- 发布时间:10/8/2008 8:58:00 AM -- 请问楼主,xquery代码究竟该放在什么地方。可以直接嵌入html吗?我试过,好像不行。有谁用过xmlspy,可以用来调试xquery吗? |
-- 作者:venee_lee -- 发布时间:10/9/2008 10:32:00 AM -- 试出来了,把xquery放在后缀为xq的文件中就可以了。我用的是altova XMLspy。感觉还是蛮方便的。 |
-- 作者:yhjhoo -- 发布时间:2/12/2009 3:44:00 PM -- 完全跟w3cshool上面的内容一样,没有任何创新啊! |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
203.125ms |