XQuery/Simile Exhibit

Motivation
You want to create a Simile Exhibit output of an XML file. To do this we will need to convert XML to JSON file format.

Method
You have a file of contributors to a book and you would like to create a map of their locations.

XQuery to Output JSON File
First we must transform our XML file into JSON format. This is a little tricky because JSON required the curly brace characters to be added to the output. The can be done by creating special variables that contain the strings.

In the XQuery header we also have to change the serialization method from our traditional XML to text/plain.

We also have to wrap our item output in a string-join function to prevent the last comma from being serialized.

JSON files are just another file format for storing hierarchical data. Just like XML. JSON is used mostly by JavaScript developers that are not either not familiar with XML or don't have XML editing tools to validate file formats. JSON does allow for nesting of complex data but does not support many XML features such as namespaces.

Unlike XQuery, JSON file formats do not permit a "dash" character in the label unless you put quotes around the label. So note that the image-url property label has quotes around it.

Sample JSON Output
{ "items" : [ { label:        "John Doe", location:    "New York, NY", "image-url": "http://www.example.com/images/john-doe.jpg" },    {  label:        "Sue Anderson", location:    "San Francisco, CA", "image-url": "http://www.example.com/images/sue-anderson.jpg" } ] }

Alternative Approach
An alternative is to use the fact that curly-braces can be escaped in XQuery by doubling. Since the output is being serialized as text, all elements will be serialised, so there is no need to serialise items separately.

Execute