Loading TOC...
Getting Started With MarkLogic Server (PDF)

Getting Started With MarkLogic Server — Chapter 6

Getting Started with XQuery

This chapter describes how to use Query Console to interactively create and execute XQuery code. This chapter includes the following sections::

XQuery Mini Tutorial

This section includes the following procedures to demonstrate how to use Query Console to take a closer look at the XQuery code in the modules from Sample XQuery Application that Runs Directly Against an App Server:

Creating and Inserting a Document into the Database

This section examines a simple XQuery program that creates a document. First, run the code as follows:

  1. Open Query Console in a browser and create a new query.
  2. Cut and paste the following code into the XQuery source editor.
    xquery version "1.0-ml";
    xdmp:document-insert("books.xml",
      <books xmlns="http://www.marklogic.com/ns/gs-books">
        <book bookid="1">
          <title>A Quick Path to an Application</title>
          <author>
            <last>Smith</last>
            <first>Jim</first>
          </author>
          <publisher>Scribblers Press</publisher>
          <isbn>1494-3930392-3</isbn>
          <abstract>
              This book describes in detail the power of how 
              to use XQuery to build powerful web applications 
              that are built on the MarkLogic Server platform.
          </abstract>
        </book>
      </books>
    )
  3. Click Run. Query Console indicated that your query returned the empty sequence.

Taking a closer look at this code, we see it uses the xdmp:document-insert function to insert a books node into a document with the URI, books.xml. The books node is in the http://www.marklogic.com/ns/gs-books namespace, which is specified with the following namespace declaration:

xmlns="http://www.marklogic.com/ns/gs-books"

For details on the use of namespaces, see Understanding XML Namespaces in XQuery in the XQuery and XSLT Reference Guide.

Querying the Document

This section examines a simple XQuery program that queries the document you previously. First, run the code as follows:

  1. Create another Query Console query.
  2. Cut and paste the following code into Query Console in the new query editor. This code is from the dump.xqy module in Sample XQuery Application that Runs Directly Against an App Server.
    xquery version "1.0-ml";
    (: dump.xqy :)
    declare namespace bk = "http://www.marklogic.com/ns/gs-books";
    
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Database dump</title>
      </head>
      <body>
      <b>XML Content</b>
    {
        for $book in doc("books.xml")/bk:books/bk:book
        return
        <pre>
            Title: { $book/bk:title/text() }
            Author: { ($book/bk:author/bk:first/text(), " ",
            $book/bk:author/bk:last/text()) }
            Publisher: { $book/bk:publisher/text() }
        </pre>
    }
      </body>
    </html>
  3. Click on the HTML button to view the output in HTML.
  4. Click Run and examine the results.

Taking a closer look at this query, we see it binds the bk prefix to the namespace in which the inserted books node resides.

declare namespace bk = "http://www.marklogic.com/ns/gs-books";

This bk prefix can now be used throughout the query to reference the namespace.

The query also defines a for clause with an XPath expression that iterates through each child element of the book element. Note that the books and book elements are in the namespace bound by the bk prefix:

for $book in doc("books.xml")/bk:books/bk:book

For details on the for clause, see FLWOR Expressions in the XQuery and XSLT Reference Guide.

The following lines include simple XPath expressions that return the text node inside each specified element. Note that each element is in the namespace bound by the bk prefix:

Title: { $book/bk:title/text() }
Author: { ($book/bk:author/bk:first/text(), " ",
           $book/bk:author/bk:last/text()) }
Publisher: { $book/bk:publisher/text() }

For details on XPath, see XPath Quick Reference in the XQuery and XSLT Reference Guide.

Modifying the Document

Create another Query Console query. Cut and paste the following code into the new query to change the text in the publisher element:

xquery version "1.0-ml";
declare namespace bk = "http://www.marklogic.com/ns/gs-books";
xdmp:node-replace(doc("books.xml")//bk:publisher, 
  <bk:publisher>Pirate's Press</bk:publisher>) 

After running this query, rerun the query in Querying the Document and notice the changed content.

Adding a New Element to the Document

Create another query in Query Console. Cut and paste the following code into the new query to add another book to the books element described in Creating and Inserting a Document into the Database:

xquery version "1.0-ml";
declare namespace bk = "http://www.marklogic.com/ns/gs-books";
xdmp:node-insert-child(doc("books.xml")/bk:books,
  <book bookid="2" xmlns="http://www.marklogic.com/ns/gs-books">
    <title>An Alternate Path to an Application</title>
    <author>
      <last>Smith</last>
      <first>Jim</first>
    </author>
    <publisher>Scribblers Press</publisher>
    <isbn>3491-3234352-1</isbn>
    <abstract>This book describes another way to use XQuery to build powerful web applications that are built on the MarkLogic Server platform.</abstract>
  </book> )

Taking a closer look at this query, we see it uses the xdmp:node-insert-child function to insert the book element as a child of the existing books element. To construct the book element in the same namespace as the books element, we explicitly declare the inserted book element to be in the same namespace as the books element:

<book bookid="2" xmlns="http://www.marklogic.com/ns/gs-books">

After running this query, rerun the query in Querying the Document.

What Next?

To learn more about MarkLogic Server, take a closer look at the MarkLogic documentation on docs.marklogic.com, including the following:

« Previous chapter
Powered by MarkLogic Server 7.0-4.1 and rundmc | Terms of Use | Privacy Policy