Hello, world! with XQuery on MarkLogic


Getting started with XQuery and MarkLogic is very easy. There is only one server to install, and no IDE is needed.

To set up a “Hello, world!” application:

  1. Download and install the MarkLogic server from http://www.marklogic.com/product/download-software.html. The MarkLogic Server is the database, search engine, and application server all in one server. There’s nothing else to set up of configure.
  2. Go to http://localhost:8001 and login using the admin user you created. This is the Administration Console where you can configure everything in your MarkLogic instance.
  3. In the left-hand pane, click on Groups -> Default -> App Servers
  4. Now in the right-hand pane click on the “Create HTTP” tab
  5. In the “Server Name” field, type “hello”
  6. In the “Root” field, type “c:/hello” (or some other available directory)
  7. In the “Port” field, type “8500″ (or some other available port)
  8. Scroll down about half way and for “Authentication” choose “application-level”
  9. Right below, for “Default user” choose “admin” (or whatever admin user you create during installation)
  10. Click OK
  11. In c:/hello create a file named “default.xqy”
  12. Edit “default.xqy to just have “Hello, world!” (surrounded by double quotes).
  13. Got to http://localhost:8500 in your browser. You should see “Hello, world!” printed out (without the quotes)

To output HTML from the page:

  1. Change “default.xqy” to have the following code:

let $message := "Hello, world!"
return
    <html xmlns="http://www.w3.org/1999/xhtml">
        <body>I'd like to say: {$message}</body>
    </html>

Make sure you include the namespace in the html tag. Otherwise you’d probably see the XML of the file in your browser, rather than the browser actually rendering the output as HTML.

To make sure that the browser correctly identifies the content as HTML and that it will be parsed and displayed correctly, you can set the content response type in the HTML response and include a DOCTYPE declaration at the top of the HTML. To do this you’ll actually need to return a sequence where the first value is a call to a MarkLogic function to set the response type, the second is the DOCTYPE declaration, and the third value is the markup itself:

let $message := "Hello, world!"
return (xdmp:set-response-content-type("text/html"),
 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
 <html xmlns="http://www.w3.org/1999/xhtml">
 <body>I'd like to say: {$message}</body>
 </html>
)

You can see the “newbie track” posts for further help in getting started with XQuery web app development.

  1. April 26, 2010 at 2:29 pm | #1

    Ryan, I love to blog so far. I’d also add a Content-Type header to the HTTP response and a DOCTYPE to the generated document in the above example. Together these two declarations will normalize some of the low-level rendering behavior across browsers. More details and background at http://markmail.org/message/iu6sh6fnhstamm3t. For example,

    let $message := “Hello, world!”
    return (
    xdmp:set-response-content-type(“text/html”),
    ”, (: …or your favorite flavor :)

    I’d like to say: {$message}

    )

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.