Formatting Results

We'll soon see more examples selecting messages from MarkLogic, but first let's look at ways to output the results more beautifully than a straight XML dump. The XQuery language provides the ability to loop over nodes and construct new nodes. Here's a query that converts the previous subject list into an HTML list:

That's a full legal XQuery program. XQuery uses XML as a native data type and that makes constructing markup like this extremely easy. The curly braces separate literal XML from enclosed expressions. The for loop processes each message and invokes the return clause once for every message.

OK, let's take a minute to talk about HTML character escaping. When producing HTML output like this you normally need to worry about special characters, otherwise you open yourself up to cross-site scripting (XSS) attacks. The XSS issue arises if, for example, there's a subject string that contains HTML tags. If not properly escaped, it'll be output raw and provide an opportunity for a malicious user to inject their own HTML content into your page. In PHP you avoid that by calling htmlspecialchars() each time you output any string variable into the page. You're never allowed to forget. So what do we do in XQuery? You don't have to do anything! The escaping happens automatically at the language level. That's a perk of using a markup-aware language.

Try it yourself:

OK, let's continue. To get a more useful HTML list, let's include the mail's sender, date, and subject together. The following prints the from/@personal attribute which is the extracted person's name, and it formats the date as year/month/day:

If we'd rather generate the result as custom XML, that's real easy:

Or, if we'd rather send this to a recipient like a JavaScript program that prefers JSON, we can do that too, using a json.xqy library whose functions we import (see https://github.com/marklogic/mljson for more about the library).

Drilling in with XPath

Constraints

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