Introduction to XQuery (II)

Source: Internet
Author: User
Tags xquery

Introduction to XQuery (II)

Author: gobitan (rain) Date: Reprinted please indicate the source of http://blog.csdn.net/gobitan in the "XQuery use entry (a)" has been using Berkeley dB XML to build an experimental XQuery environment, now you can start learning XQuery. This article describes how to use XPath to query elements in XML documents, filter query results using predicates, and package and sort query results. For the XQuery usage described later in the experiment, you must first prepare an XML document. (For more information, see the putdocument command. Here, the document content is not consistent with the actual content for the convenience of description ). The document content is a list of books, and the root of the XML document is <booklist>. First, create a container named xmldb. The concept of the container here is equivalent to a database in the relational database, so the XML document stored in the container is equivalent to a table in the relational database. In order to distinguish the input command from the result of the system prompt, the gray area indicates the input command, and other indicates the system prompt. Dbxml> createcontainer xmldbcreating node storage container with nodes indexed the gray-colored command above creates a container named xmldb. The following is a prompt after the system operation is successful. Dbxml> putdocument Booklist. <booklist> <book lang = "en"> <ISBN> 7121021161 </ISBN> <Name> the art of Unix programming </Name> <author> Eric Steven Raymond </author> <PRESS> Addison-Wesley professional </PRESS> <price> 59.00 </price> </book> <book lang = "en"> <ISBN> 9787121039690 </ ISBN> <Name> inside widows Operation System </Name> <author> russinovich, m. e. </author> <author> Solomon, D. a. </author> <PRESS> Micros Oft press </PRESS> <price> 99.00 </price> </book> <book lang = "ZH"> <ISBN> 0131411551 </ISBN> <Name> UNIX Network Programming </Name> <author> by W. richard Steven S </author> <author> Bill Fenner </author> <author> Andrew M. rudoff </author> <PRESS> Addison-Wesley professional </PRESS> <price> 79.00 </price> </book> </booklist> 'document added, name = Booklist. the preceding commands in XML are used to save the prepared XML document to the xmldb container by using the putdocument command. The document name is Booklist. xml. Note that the createcontainer command in the previous step will default the created container to the current container. Therefore, you do not need to specify the container to be saved when saving the document. (1) Using XPath to query element XPath in XML documents is a path language used to locate element positions in XML documents. It can be used to accurately locate any element or attribute in XML documents. XPath is easy to use. It separates the hierarchical structure of XML with a backslash. For example, the following example is to query the name of a book. Based on its location in the XML document, you can go from the root element booklist to the book element at a time, and then to the name, as shown below: dbxml> query'doc ("xmldb/Booklist. XML ")/booklist/book/name '3 objects returned for eager expression' DOC (" xmldb/Booklist. XML ") /booklist/book/name 'dbxml> Print <Name> the art of Unix programming </Name> <Name> inside widows Operation System </Name> <Name> UNIX Network Programming </Name> (2) use the XQuery predicate to filter the query results. The names of all books in the XML document are queried. For example, the language is Chinese. Then we can use predicates to filter the query results. You can use a predicate by adding an expression in square brackets to the element to be filtered. The expression starts with the @ symbol and is attached with a Boolean expression. For example, to query only Chinese books, the Boolean expression is lang = "ZH". The complete query statement is as follows: dbxml> query'doc ("xmldb/Booklist. XML ")/booklist/book [@ lang =" ZH "]/name '1 objects returned for eager expression' DOC (" xmldb/Booklist. XML ")/booklist/book [@ lang =" ZH "]/name 'dbxml> Print <Name> UNIX Network Programming </Name> as shown in the preceding results, the query statement filters out the names of books whose lang attribute is not Zh. (3) using the XQuery command to wrap the query results in actual applications, it is often necessary to wrap the query results to form XML data in a new format. For example, add the <HTML> tag to wrap it into an HTML webpage, or add the root element to form another XML document. Here, the query result of the book name is wrapped into an order, that is, the <order> root element is added. The package format is written first, and then the query result is put into it through. Dbxml> query' <order> {DOC ("xmldb/Booklist. XML ") /booklist/book [@ lang = "ZH"]/name} </order> '1 objects returned for eager expression' <order> {DOC ("xmldb/Booklist. XML ") /booklist/book [@ lang = "ZH"]/name} </order> 'dbxml> Print <order> <Name> UNIX Network Programming </Name> </order> A New XML document is formed by the query result after the package. (4) using the XQuery command to sort the query results when using SQL statements, sorting the query results can be said to be more common. Here we should naturally introduce how to implement this function in XQuery. Sorting the XQuery query results is more complex than the previous ones. The example below shows that it also involves the for, where, order by, and return keywords. Here we will not introduce more keywords for, where, and return. If you have used SQL, you should be very familiar with order by. However, note that the reference of sorting elements (equivalent to fields in a relational table) in XQuery is somewhat different from that in SQL. It requires a $ prefix before the element, followed by the XPath language specification. Dbxml> query' for $ book in DOC ("xmldb/Booklist. XML ") /booklist/bookwhere $ book/@ lang = "en" order by $ book/namereturn $ book/name '2 objects returned for eager expression' for $ book in DOC ("xmldb/ booklist. XML ") /booklist/bookwhere $ book/@ lang = "en" order by $ book/namereturn $ book/name 'dbxml> Print <Name> inside widows Operation System </Name> <name> The Art of Unix programming </Name> describes the common usage of XQuery. Method. We will introduce some more in-depth applications later. About the author: Hu Jiahui, net name: Yu currently focuses mainly on XML document management and related technologies.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.