This section describes the following procedures to get started using XQuery in MarkLogic Server:
Be sure to complete each procedure in the order presented.
As part of the XQuery standard, the W3C working group has assembled a set of use cases that demonstrate how XQuery can be used to accomplish a variety of sample tasks. As part of its installation process, MarkLogic Server provides a workspace for Query Console containing working demonstrations of these use cases. The use cases provide an excellent starting point for familiarizing yourself with the XQuery language and with some important aspects of MarkLogic Server.
To explore the use cases, complete the following steps:
/Samples/w3c-use-cases.xml
.If MarkLogic is running on the same machine as your browser, you can navigate to this directory (for example, c:/Program Files/MarkLogic/Samples
on Windows or /opt/MarkLogic/Samples
on Linux), but if MarkLogic is installed on a remote server, then you must copy this file from the server to the machine in which your browser is running, and then navigate to the directory where you copied the file.
A confirmation message displays indicating the documents have been loaded.
You can complete these procedures in any order.
This procedure focuses on the first use case topic, 1.0: Exemplars. You can view the source XML for any of the use cases.
This procedure focuses on the first use case in Exemplars. You can view any use case in other topics as well.
To view the use case, complete the following steps:
Lists books published by Addison-Wesley after 1991 including their year and title.
Notice that XQuery comments are wrapped in smiley faces: (: comment :)
This procedure focuses on the first use case in Exemplars. You can edit any use case in the list.
To edit a use case, complete the following steps:
where $b/publisher = "Addison-Wesley" and $b/@year > 1991 |
where $b/publisher = "Addison-Wesley" and $b/@year > 1993 |
You may change the source as much as you like. Explore and customize each use case as thoroughly as possible. You can reload the workspace from the Sample directory to restore it.
The App-Services
App Server at port 8000 hosts Query Console and other MarkLogic applications. The App-Services
App Server also serves as an HTTP App Server, an XDBC App Server, and as a REST API instance. XQuery, Node.js, XCC, and REST applications can be run directly on the App-Services
App Server.
By default, the App-Services
App Server uses the Documents
database. The MarkLogic APIs provide mechanisms for changing the database when necessary.
The Sample XQuery Application described below is run on the App-Services
App Server.
In this section, you create a sample XQuery application. This is a simple browser-based application that runs against an HTTP App Server, and that allows you to load and modify some data. First you will create the App Server, and then create the applicaton. This procedure includes the following parts:
In this section, you create a new HTTP App Server. An App Server is used to evaluate XQuery code against a MarkLogic database and return the results to a browser. This App Server uses the Documents database, which is installed as part of the MarkLogic Serverinstallation process. In Sample XQuery Application that Runs Directly Against an App Server, you use this App Server to run a sample XQuery application.
To create a new App Server, complete the following steps:
TestServer
.This is the name that the Admin Interface uses to reference your server on display screens and in user interface controls.
/space/test
(or whatever directory you want for your App Server root, for example c:/space/test
on a Windows system).By default, the software looks for this directory in your MarkLogic Server program directory, as specified in the Installation Guide. But it is much better practice to specify an absolute path (such as C:\space\test
on a Windows platform or /space/test
on a Linux platform).
8005
(or whatever port you want to use for this App Server).The following screen shows an HTTP server with these values:
application-level
.This makes it so you do not need to enter a username or password against this App Server. If you want to enter a username and password, leave this setting as digest
.
admin
in parenthesis) as the Default User.The following screen shows an HTTP server with these values
To create and run the sample XQuery application, complete the following steps:
/space/test
. If you are using a different directory (for example, c:/space/test
), just make everything relative to that directory./space/test
(or whatever directory you want).load.xqy
in the /space/test
directory. .xqy
file:xquery version "1.0-ml"; (: load.xqy :) 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> ), <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Database loaded</title> </head> <body> <b>Source XML Loaded</b> <p>The source XML has been successfully loaded into the database</p> </body> </html>
dump.xqy
in the /space/test
directory..xqy
file: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> } <a href="update-form.xqy">Update Publisher</a> </body> </html>
update-form.xqy
in the /space/test
directory..xqy
file:xquery version "1.0-ml"; (: update-form.xqy :) declare namespace bk="http://www.marklogic.com/ns/gs-books"; <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Change Publisher</title> </head> <body> { let $book := doc("books.xml")/bk:books/bk:book[1] return <form action="update-write.xqy"> <input type="hidden" name="bookid" value="{ $book/@bookid }"/> <p><b> Change publisher for book <i>{ $book/bk:title/text() }</i>: </b></p> <input type="text" name="publisher" value="{ $book/bk:publisher/text() }"/> <input type="submit" value="Update publisher"/> </form> } </body> </html>
update-write.xqy
in the /space/test
directory..xqy
file:xquery version "1.0-ml"; (: update-write.xqy :) declare namespace bk="http://www.marklogic.com/ns/gs-books"; declare function local:updatePublisher() { if (doc("books.xml")) then let $bookid := xdmp:get-request-field("bookid") let $publisher := xdmp:get-request-field("publisher") let $b := doc("books.xml")/bk:books/bk:book[@bookid = $bookid] return if ($b) then ( xdmp:node-replace($b/bk:publisher, <bk:publisher>{ $publisher }</bk:publisher>) , xdmp:redirect-response("dump.xqy") ) else <span>Could not locate book with bookid { $bookid }.</span> else <span>Unable to access parent XML document.</span> }; <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Update In Process</title> </head> <body> Attempting to complete update and redirect browser to detail page. <p> If you are seeing this page, either the redirect has failed or the update has failed. The update has failed if there is a reason provided below: <br/> { local:updatePublisher() } </p> </body> </html>
Test
directory:.xqy
extension, not the .txt
extension.To load the source XML, complete the following procedure:
To generate a simple report from the newly loaded XML, complete the following steps:
To submit new information to the database, complete the following steps:
This action automatically calls update-write.xqy
, which updates the publisher element in the database, and then redirects the browser to dump.xqy
which displays the updated book information.