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>
<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.
Copyright by Gerald Beuchelt.
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
In addition, my opinions can change. This weblog provides a momentary snapshot of such opinions. Existing posts that were written in the past do not necessarily reflect my current thoughts and opinions.
For the purposes of attribution, the author is "Gerald Beuchelt" and attribution shall provide a (clickable, where possible) URL to this site.
© 2010, Gerald Beuchelt
E-mail