This chapter describes how to use Query Console to interactively create and execute XQuery code. This chapter includes the following sections::
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:
This section examines a simple XQuery program that creates a document. First, run the code as follows:
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> )
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.
This section examines a simple XQuery program that queries the document you previously. First, run the code as follows:
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>
HTML
button to view the output in HTML.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.
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.
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.
To learn more about MarkLogic Server, take a closer look at the MarkLogic documentation on docs.marklogic.com, including the following: