RELAX-iNG Your Schemas

My employer requires that all services have an XML Schema published and that you validate your XML before sending it out.  That’s really a good idea — that way you know your data is good and you have a schema where other people can validate that.

The bad part is, though, Schemas stink.  They are hard to write, hard to understand, and sometimes they just don’t make sense.  Fortunately, RELAX NG schemas are easier to write and understand.  And most libraries now can validate against a RELAX NG schema.   But my users may not be expecting a RELAX NG schema or perhaps their parser library doesn’t support it.  Do I have to settle for a format that I don’t like?

That is where the trang library comes in.

I created a RELAX NG schema and use trang to convert that to an XML (XSD) Schema.  That schema is what I send to my users.  And, just so we are validating from the same source, I also validate internally with the XSD schema.  Yet I only change the RELAX NG file and let trang convert it for me.

Did I say that the generated XSD Schema is extremely clean and doesn’t even smell auto-generated?  That’s the best part!

Here is the ANT task that  I wrote to convert my RELAX NG schema to XSD:

     <target name="schema"
depends=”compile”
description=”Builds the XSD Schema from Relax-NG schema”>

<java classname=”com.thaiopensource.relaxng.translate.Driver” fork=”yes”>
     <arg line=”schemas/Relax-input.rng”/>
     <arg line=”schemas/XSD-output.xsd”/>
     <classpath>
       <pathelement location=”lib/trang.jar”/>
    </classpath>
    </java>
</target>


Powered by ScribeFire.

One Response to “RELAX-iNG Your Schemas”

  1. ghostetler Says:

    This post really speaks to me… :)

Leave a Reply

You must be logged in to post a comment.