Automating RNG to XSD conversion in NetBeans

Working currently on an RelaxNG project, I needed to automate conversion of RNG schemas to a W3C compliant schema in NetBeans. The tool I used to perform the transform is Trang. I added this macro to the build.xml file:

<macrodef name=”rng2xsd” description=”Conversion from RNG to XSD schemas”>
    <attribute name=”rng” />
    <attribute name=”xsd” />
    <sequential>
        <echo message=”Convert RNG schema (trang/oxygen): @{rng}”/>
        <java classname=”com.thaiopensource.relaxng.translate.Driver”
               failonerror=”true” maxmemory=”128m” fork=”true”>
            <arg value=”-I”/>
            <arg value=”rng”/>
            <arg value=”-O”/>
            <arg value=”XSD”/>
            <arg value=”@{rng}”/>
            <arg value=”@{xsd}”/>
            <classpath>
                <pathelement location=”resources/tools/trang-20081028.jar”/>
            </classpath>
        </java>
    </sequential>
</macrodef>

All necessary libraries reside in the ./resources/tools directory. Now, in order to use this macro on a number of RNG files, I decided to use the <for> directive from ant-contrib. James Allen has good instructions on how to integrate ant-contrib within NetBeans (or arbitrary ant environments) without having to drop the ant-contrib Jar into the ant/NetBeans installation.

<target name=”convertRng2Xsd”>
    <echo message=”Converting RNG Schemas…”/>
    <mkdir dir=”${xsd-schemas}”/>
    <for list=”${rng-files}” param=”file”>
        <sequential>
            <rng2xsd rng=”${rng-schemas}/@{file}.rng” xsd=”${xsd-schemas}/@{file}.xsd” />
        </sequential>
    </for>
</target>

Here I am iterating over the ${rng-files} property that contains a comma delimited list of the RNG files you want to convert (without the .rng extension). I filled this through <pathconvert>:

<pathconvert property=”rng-files” pathsep=”,”>
    <mapper>
        <chainedmapper>
            <flattenmapper />
            <globmapper from=”*.rng” to=”*” />
        </chainedmapper>
    </mapper>
    <path>
        <fileset dir=”resources/schemas” includes=”*.rng” />
    </path>
</pathconvert>

Obviously, these XSDs can then be used with any other tools, such as JAXB.

3 thoughts

  1. I’m not sure why you needed to convert your rng to xsd (gasp), but JAXB can process rng/rnc as well as xsd, and dtd.

  2. Yes, I am aware of that, but XSD is unfortunately a requirement for the project. This way I can create my XSD artifacts on the fly, and go through the accepted XSD JAXB binding, but still work off the RNG schemas. It is sad that after so many years RNG has still not made it into the mainstream of the corporate world.

  3. BTW – quick update: JAXB 2.x RNG support is really not too great at this time. Just for giggles I used the -relaxng option on the original files, and all kinds of things started to fall apart. So at this time the RNG -> XSD -> Class sequence seems to be the most stable.

Leave a Reply to RShoemaker Cancel reply

Your email address will not be published. Required fields are marked *