Thursday, 25 March 2010

MarkLogic: Loading and using XML Schemas (Part Two : Schema Aware XQuery)

Now the schema is loaded into MarkLogic, here are some rough notes on handling the "Schema Aware" facets of the server. Again, this information is pulled together from various MarkMail posts and a bit of trial and error:

Executing this should return the element. It should be noted that the formatted example, all the tags appear with lower-case element names. These will need to be changed back if you're following with the example schema and code.

To prove that the content is being validated against the schema, changing the date to something nonsensical (add a couple of random characters in, for example), should yield something like this:

[1.0-ml] XDMP-VALIDATEBADTYPE: (err:XQDY0027) validate strict { $input } -- Invalid node type: emp:DateOfBirth lexical value "2006-05-0ss4" invalid for expected type #xs:date at /emp:Employees/emp:Employee/emp:DateOfBirth using schema "rdl.xsd"

Namespace Prefixed Example

This example may be more useful if you're dealing with elements whose content comes from multiple namespaces:

MarkLogic: Loading and using XML Schemas (Part One)

These are some rough notes on techniques for using XML Schemas with MarkLogic.

Loading an XML Schema using cq:

The quickest way I've found to get a schema into your local MarkLogic instance is to copy your xsd file into your MarkLogic Docs folder (I also made a folder called schemas), then open a cq instance (or DQ if you prefer), select Schemas as your content source and use some XQuery like this:

Loading an XML Schema using XCC/J:

Another method would be to use XCC/J - I hope to write another post on this technique at another time with a more detailed example (note this method is untested at the time of writing):

After the cq process has been executed, running either:


Should show you the schema.

It's important to note that when MarkLogic loads schemas into its database, it will put in any values that have defaults on load. This caused some confusion when I was testing the "Schema Aware" aspects of the server. I added the following attribute to my schema's parent element:

Here's a very basic example of a schema layout:

Most of this information is available elsewhere, so this post will hopefully serve to pull a few pieces together in the interests of saving time.

Recommended reading:

Wednesday, 24 March 2010

MarkLogic: testing for a specific word in local content

This returns a simple boolean response based on whether a specific word can be found in an element bound to a variable