views:

1101

answers:

5

A brief search shows that all available (unix command line) tools that convert from xsd (XML Schema) to rng (RelaxNG) or rnc (compact RelaxNG) have problems of some sort.

First, if I use rngconv:

$ wget https://msv.dev.java.net/files/documents/61/31333/rngconv.20060319.zip
$ unzip rngconv.20060319.zip
$ cd rngconv-20060319/
$ java -jar rngconv.jar my.xsd  > my.rng

it does not have a way to de-normalize elements so all end up being alternative start elements (it also seems to be a bit buggy).

trang is an alternative, but it doesn't support xsd files on the input only on the output (why?). It supports DTD, however. Converting to DTD first comes to mind, but a solid xsd2dtd is hard to find as well. The one below:

 $ xsltproc http://crism.maden.org/consulting/pub/xsl/xsd2dtd.xsl in.xsd > out.dtd

seems to be buggy.

All this is very surprising. For all these years of XML (ab)use, there no decent command line tools for these trivial basic tasks? Are people using only editors? Do those work? I much prefer command line, especially because I'd like to automate these tasks.

Any enlightening comments on this?

+2  A: 

Converting XSD is a very hard task; the XSD specification is a bit of a nightmare and extremely complex. From some quick research, it seems that it is easy to go from RelaxNG to XSD, but that the reverse may not be true or even possible (which explains your question about Trang).

I don't understand your question about editors - if you are asking if most people end up converting between XSD and RNG by hand, then yes, I expect so.

The best advice may be to avoid XSD if possible, or at least use RNG as the definitive document and generate the XSD from that. You might also want to take a look at schematron.

Adrian Mouat
Yes, the reverse direction (rng/rnc2xsd) is fine. trang works well for that.
kmt
WRT editors, yes, that's what I meant. I assume people in the XML world use mostly editors, otherwise I can't explain the lack of command-line tools. I find it interesting that the XML world is so far from the unix philosophy of simple tools that do one thing well.
kmt
WRT avoiding XSD, I'd love to. That's why I'm interested in the much-easier-to-read RNC format. I don't have a choice, however.
kmt
Not sure if I even understand what schematron is and how it can help.
kmt
Schematron probably can't help in your case. It basically allows you to make assertions about a document; e.g. element a has a b child.See http://www.zvon.org/xxl/SchematronTutorial/General/contents.html
Adrian Mouat
Also, I'm not convinced there is a lack of command line tools - I used the libxml stuff quite a lot. There's also XMLStarlet http://xmlstar.sourceforge.net/, but I haven't tried it.
Adrian Mouat
Adrian, thanks for all the comments! (Sorry I can't upmod you, not enough karma yet). Although xmlstar cannot help for my particular problem it certainly looks like a useful tool.
kmt
No prob. Good luck!
Adrian Mouat
As @Adrian said, XSD is more expressive than RNG, and so converting from the former to the latter is never going to work reliably. Hardly surprising, then, that the tools to do aren't very good.
skaffman
A: 

Again, regarding editors: I see that there's no way to do this with Oxygen which seems to be a popular tool.

kmt
A: 

Another possibility might be http://www.brics.dk/schematools/, but I didn't try it yet.

cmarqu
A: 

True, trang does not accept xsd on the input side. Trang can however take a set of xml files which should meet the spec and generate a rnc or rng schema which they would all be valid against.

Downsides:

  • It requires many compliant xml files (I'd imagine the more the better)
  • Resulting schema could probably still use some tweaking.

Sample Case:

If my compliant xml files are stashed in 1.xml 2.xml 3.xml 4.xml 5.xml

the following command would tell trang to output a rnc schema that would be valid for all of them:

java -jar trang.jar -I xml -O rnc 1.xml 2.xml 3.xml 4.xml 5.xml foo.rnc

Conclusion

If you have a nice test set of xml files which meet your schema (or you can easily create them) this may be the best option available.

I wish you the best of luck.

neozen
+1  A: 

There is an online converter (XSD -> RNG) added to the list at http://relaxng.org/#conversion. I have tried to convert maven-v4_0_0.xsd for validation of pom.xml files in emacs, without any luck though. The site also contains the XSL stylesheet that you could use with xsltproc, can't vouch for the quality of the outcome...

antonj
Thanks for mentioning your use case, I was just about to go down the same rabbit hole with pom.xml validation in Emacs.
Steven Huwig