Stay on top of everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up


Stay on top of everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up


Stay on top of everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up


Stay on top of everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up

Developing XQuery Applications: Part 1

Dave Cassel and Clark Richey
Last updated October 19, 2015

This version of the tutorial applies to MarkLogic 8 and later. For MarkLogic 6 and MarkLogic 7, see the earlier version.

This tutorial will walk you through the creation of a simple MarkLogic-based application. Before starting, you should download and install the latest version of the MarkLogic server. Installation instructions for the server can be found here.

The first thing we are going to need for this application is some XML content. Without any content our application is going to be rather hard to test and somewhat dull. For this tutorial we're going to use some Shakespeare plays in XML format. At this point all you need to do is download the zip file. We'll get back to our content in a little while.


While Shakespeare's works are downloading, let's set up MarkLogic for our project. All we need for now is a database with at least one forest. (A forest actually holds the data; a database may consist of multiple forests and may be split among multiple hosts.)

We'll be creating a few files for our project, so let's start by creating a directory for this project. In your project directory, create two subdirectories: config and src. We'll add files as we go.

To configure our project, we'll use the MarkLogic Management API. We'll start with a configuration that we can pass to the API. Our first step is to create a database. Make a file in your config directory called content-db.xml and paste this into it:

Our XML file holds the configuration we want; to tell MarkLogic to use it, we'll run a curl command. (curl is a commonly-used command line tool for sending HTTP messages; if you prefer a different tool, you can use that instead.) We'll direct this command to port 8002, as that's where MarkLogic's Manage application server listens.

This tutorial assumes the admin password is "mypassword"; naturally you'd use something more secure for real deployments. Substitute your password accordingly.

Having run this command, you now have a database called shakespeare-content. You can verify this by looking at the Admin UI on port 8001.

As mentioned earlier, we also need a forest, which will physically hold our data. Create another file in your config directory called content-forest.xml and paste the following:

An important note about this file: the "host" element needs to match the name that MarkLogic knows for the host. That means we can't just use "localhost" here. If you're not sure what name MarkLogic knows, check the Admin UI on port 8001 or use the Management API -- my host is macpro-2396.

Loading Our First Files

Now that we have a database, it's time to load up some data. For this task, we'll use the MarkLogic Content Pump. Download and unzip MLCP. This tool has a wide range of capabilities, but we'll do something pretty straightforward with it: we'll point MLCP to the zipped bunch of Shakespeare documents that we downloaded earlier and load them as separate XML documents in our MarkLogic database.

You may need to adjust the path to where you installed MLCP. We use port 8000 because it has a built-in XDBC server, which MLCP needs. Each application server has a content database associated with it. We use the -database parameter to ensure the content gets to our shakespeare-content database.

Getting Started with Query Console

OK. That was easy enough. However, like most programmers, I'm somewhat skeptical. Sure, MLCP's output looks like it did something, but how do I know it loaded the documents? How do I know they were loaded correctly? Well, we can use Query Console to help us verify that our documents are in the database (and to do lots of other stuff as well). Query Console is basically a web-based programming environment for MarkLogic server. To access Query Console all you need to do is point your browser to http://localhost:8000/qconsole. Really. That's it. Go ahead, do it. I'll wait...Super! You're back. You should now seem something very much like this:

Query Console (thumbnail)

The first thing we want to do here is to choose which of our databases we want to set as the target for our actions in Query Console. We choose the shakespeare-content database by selecting it from the dropdown list labeled "Content Source". Now we can use Query Console to verify that our play was loaded into the database. Click on the "explore" button near the top left of the screen, just to the right of the dropdown box you just used to select our Shakespeare database. In the bottom half of your screen you should now see a list of all documents contained in our database, as well as a little bit of information about the documents. If you click on the document link you will be presented with the actual content of the document. Now we know for sure that we have the database configured and our plays loaded.

Lastly, let's get a quick glimpse of the power and utility of QueryConsole by using it to execute some XQuery for us. In the work space on the top left of the screen (called a buffer), delete all of the text except for the XQuery version statement. Now, let's enter a bit of simple XQuery to show us an HTML rendering of Speakers and their Lines. Enter the XQuery found below into the text area in QueryConsole where you just deleted the default text:

Now, select the "HTML" button and then click on the "Run" button to see the results. Experiment with the Auto and Raw buttons to see how the results are presented.

That wraps up part one of this tutorial! In this section you've learned the fundamental skills necessary to begin programming with the MarkLogic server, from installation and configuration through data loading and executing a simple query. In part two, we'll see how to use XQuery modules to build a dynamic web page. Until then, don't be afraid to keep exploring on your own and don't forget that there is a ton of helpful documentation available here at

Continue on to Part 2.

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.


The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • Any further information using MLCP in Mac OS 10.11 with ML 8.0-5 ?
  • 1. Impact of Mark logic issues in the overall support perspective for a customer
  • 1. Mark Logic – how reactive they are with answering and updating queries – how quickly will they move to fix some of the issues with the customer. Usual turnaround time.
  • i'm not able to see information studio in my ML local host 8000 page.. kindly any one could help me out??
    • I'll make a note to update the page, but in MarkLogic 8, go to http://localhost:8000/appservices.
  • I'd like to suggest that you change the sample query at the end of this post. doc()//SPEECH matches over 31000 elements, and when I attempted to run it, my browser window just hung, on both Chrome and FF. At first I thought that it was the ML server that was slow, but finally figured out it was that the browser was trying to load over 62000 elements into the results window, and I guess it does some JS magic on each one (syntax highlighting and whatnot). Regardless of the cause, it definitely could leave someone with a bad first impression. You could change it to something like this, perhaps (not as dramatic, but it still illustrative): ``` xquery version "1.0-ml"; for $speakers in distinct-values(doc()//SPEECH/SPEAKER) order by $speakers return string($speakers) ```
    • Agreed and fixed to just pluck out the first 100 speeches.
  • "That about **warps** things up for now!" ?? What, are you a Star Trek fan?
  • There was no forest attached with the database-Shakespeak . It would result in an error.