Saxon Jing

Saxon Jing

Saxon Jing is a small open source Java library bridging the Saxon Processor with James Clark's Jing Validator enabling one to validate XML documents against Compact Syntax RelaxNG Schemas in XSLT and XQuery via XPath Functions.

If you need to validate thousands of documents against a RelaxNG Schema or need to add RelaxNG validation into your existing XML based workflow this could come in handy.

1. XSLT and XQuery Function Signatures

declare namespace rng = "http://relaxng.org/ns/structure/1.0";

declare function rng:schema(
  $uri as xs:string
) as function(node()) as empty-sequence() external;

declare function rng:schema-report(
  $uri as xs:string
) as function(node()) as element(report) external;

The $uri parameter can either be a path to a RelaxNG Schema on disk (relative or absolute) or a fully qualified URI to a resource, for instance a HTTP URL or a JAR reference.

Calling either one of these functions will fetch and compile the RelaxNG Schema and subsequently return a function which takes either a document-node() or an element() node.

This returned function is essentially a validator which you can use as many times as you like over as many documents as you like within the same XSLT or XQuery process. The Schema is only compiled once!

The function produced from rng:schema returns an empty-sequence() if validation against the node was successful but will throw an error if the document was invalid.

The function produced from rng:schema-report will return a report element which details all validation issues (if any) that occurred whilst validating a document, exactly what the errors were and where they were found. This function should never throw an error and a successful validation will simply produce an empty <report /> element.

A failed report element, will look something along the lines of:

<report>
  <error>
    <message column-number="20" line-number="2">
      character content of element "date" invalid; must be an ISO date
    </message>
    <message column-number="24" line-number="3">
      character content of element "integer" invalid; must be an integer
    </message>
    <message column-number="10" line-number="4">
      element "blurb" not allowed anywhere; expected element "text"
    </message>
    <message column-number="8" line-number="5">
      element "data" incomplete; missing required element "text"
    </message>
  </error>
</report>

2. Example XSLT stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rng="http://relaxng.org/ns/structure/1.0"
  xmlns:fn="http://www.w3.org/2005/xpath-func