You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by xi...@xml.apache.org on 2004/03/24 15:16:39 UTC

[Xindice Wiki] New: MultipleXMLSchemaValidation

   Date: 2004-03-24T06:16:35
   Editor: 217.40.175.238 <>
   Wiki: Xindice Wiki
   Page: MultipleXMLSchemaValidation
   URL: http://wiki.apache.org/xindice/MultipleXMLSchemaValidation

   no comment

New Page:

Multiple schema validation is easy using the Namespace Routing Language (NRL). I am using this technology every day now and find it a godsend and wonder why others are not shouting from the rooftops about it.

1st download Jing from James Clark website, which is a RelaxNG processor with NRL support.

2nd define an NRL rules document

<rules xmlns="http://www.thaiopensource.com/validate/nrl">
<namespace ns="http://relaxng.org/ns/structure/1.0">
<validate schema="RELAXNG.RNG"/>
</namespace>
<namespace ns="http://www.w3.org/1999/XSL/Transform">
<validate schema="XSLT.RNG"/>
</namespace>
</rules>

the above relates the various namespaces with RelaxNG files, which come with the Jing build. Of course you can define your own schemas, using either RelaxNG or XML Schema (as Jing supports XML Schema via xerces).

3) run Jing from the commandline, supplying both the NRL document as schema, and the XML document you want to process.

Note-Jing ignores hints contained in the XML document, such as xsi:schemaLocation.

Thats it.....lets try it in practice, lets suppose we want to validate the following XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE date SYSTEM "address.dtd">
<address xmlns="http://www.ruminate.co.uk/address">
<first>Gabriela</first>
<last>Kratinova</last>
<phone>5555555555</phone>
</address>

the <address/> element is under the http://www.ruminate.co.uk/address namespace, so we need to ensure that our rules are correctly configured in our NRL document.


<rules xmlns="http://www.thaiopensource.com/validate/nrl">
<namespace ns="http://relaxng.org/ns/structure/1.0">
<validate schema="RELAXNG.RNG"/>
</namespace>
<namespace ns="http://www.w3.org/1999/XSL/Transform">
<validate schema="XSLT.RNG"/>
</namespace>
<namespace ns="http://www.ruminate.co.uk/address">
<validate schema="myaddressschema.xsd"/>
</namespace>
</rules>

now invoking jing with the following commandline;

java -jar /path-to-jing/jing.jar nrldocument.nrl address.xml

would upon finding the www.ruminate.co.uk/address namespace correctly apply the myaddressschema.xsd (XML Schema) schema. Since we always want to make things easy on ourselves why not setup Ant to process


<?xml version="1.0"?>
<project name="Backup Web Application" default="build">
<description>Backup web server distribution and associated database</description>

<property name="nrl.document" value="nrldocument.nrl"/>

<target name="build">
<java jar="../path-to-jing/jing.jar"
fork="true"
failonerror="false">
<arg value="${nrl.document}"/>
<arg value="address.xml"/>
</java>
</target>
</project>

This build file would validate any namespace it has a definition to process.....

Additionally NRL can be configured for concurrent schema validation, for example we might want to check the same namespace with multiple schemas....

All we have to do is define the rule correctly

<namespace ns="http://www.ruminate.co.uk/address">
<validate schema="myaddressschema.xsd"/>
<validate schema="myaddress.rng"/>
</namespace>

The above will validate all the http://www.ruminate.co.uk/address namespace elements against the xml schema, then the RelaxNG schema....neat.

note- if you have empty or undefined namespaces you might have to add the following XML snippet underneath the <rules/> element

<anyNamespace>
<allow/>
</anyNamespace>

this will explicitly instruct Jing to pass over elements which have an undefined namespace, or are under namespaces that are not defined in the NRL.

Namespace routing is spectacularly easy in its simple mode, NRL has plenty more in the tank though....and you should take a look at it as it is now under OASIS guidence. Personally I can see this emerging as an interesting processing model approach as well...that is instead of xml schema processing, why not attatch processing...to a processor !