Wednesday, May 9, 2012

Converting XML Schema to Relax NG

Needed to convert W3 XML Schema for web.xml files to Relax NG so that I can edit those files in emacs with XML element expansion and syntax highlighting provided by nxml mode. I found a lot of references on the Internet for Sun's XSD to RNG converter. However, most links pointing to Sun website were broken. Finally, I found that RNG converter is a part of the Kohsuke Kawaguchi's MSV project. After installing rngconv.zip, I downloaded web-app_2_4.xsd, j2ee_1_4.xsd, jsp_2_0.xsd from Sun's XML Schemas for J2EE and converted the first one for use with nxml.

One little quirk was to add in the RNC schema a definition for xsi:schemaLocation attribute to the definition of element web-app

namespace xsi = "http://www.w3.org/2001/XMLSchema-instance"
element web-app {
attribute xsi:schemaLocation { "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" },
...
2012 update: Since Oracle bought Sun, the MSV RNG Convertor is even harder to find. I found the download link at nightly rngconv.20060319.zip. Also, in j2ee_1_4.xsd, you will need to uncomment the following statement:
xsd:import namespace="http://www.w3.org/XML/1998/namespace" schemalocation="http://www.w3.org/2001/xml.xsd"
Download the result from Google Docs: web-app_2_4.rnc

1 comment:

Adam Faryna said...

I trying to do the same procedure with web-app_2_4.xsd.

rngconv produces RNG format schema emacs accepts only RNC so futher conversion is needed.

I used trang to this:
trang -I rng -O rnc web-app_2_4.rng web-app_2_4.rnc

But result still does not meet expectations. Here is the result web-app_2_4.rnc.

Any ideas?