Fog Creek Software
Discussion Board

XML Schema & XSD, round II... transform XSD?

Again, I seek advice in profound and abject humility (yeah, right)...

I'll simply ask if anyone knows about the following: what tools exist that will transform either an XML document or an XML schema into another document form?

Analogy: in web browsers there is the technology XSLT. It is apparently the successor to CSS and allows data centric, non visually embellished XML to be displayed "prettily" in a web browser according to style information that has bearing on the tags used in the XML. However, (near as I can tell) this transformation is only a visual rendering thingie.

So likewise, in a text processing sense, what exists that is a comparable way to transform XML (and preferably an XML schema, XSD) into a different language altogether that is not XML?

My thought is that if there is some way based upon open sourced (or even commercial) tools to pre-process the XSD into (say) a simpler textual form, then I am most of the way toward transforming the XSD schemas into executable source code.

The ultimate purpose being to write an XML generator class library (in my language of choice, Delphi) that generates correct XML based upon the schemas.

I have something like 300 .XSD files, each of which represents some distinct type of XML document-cum-object that I need to pass to an external system. What I eventually want to wind up with is a unique Delphi class type corresponding to each XSD file, with properties corresponding to XML entities and to each attribute of those XML entities.

Hand translation seems pretty stupid to achieve this. And I don't want to embark on writing an XSD to Delphi translater if something already exists to do this.

All further advice appreciated. Thx.

Bored Bystander
Tuesday, February 10, 2004

You can use SAX (Simple API for XML), which is sort of a "de-facto" interface, and is implemented in many languages.  I don't know any Delphi, but a quick google for "sax delphi" yields .

With SAX, you provide a "content handler" object.  You tell the SAX parser about your source XML (like your .xsd file) and your content handler.  As the parser parses, it calls your content handler for things like begin tag, end tag, text data, etc.  Your content handler can do whatever you want:  in your case, making some sort of human-friendly output from that XSD.

Tuesday, February 10, 2004

XSLT will transform XML into any arbitrary text. It's not just for display or transform to HTML.

You could also use something like CodeSmith:

if you're on Windows. I worked on the dearly departed Gen<X> from DevelopMentor; we had a lot of success using the existing MSXML parser and some JScript code to spit out code generated from schemas.

The other option is to just write some code to do the transform in your language of choice. Martin Fowler's the one who decided that Ruby made a better transform language than XSLT:

Chris Tavares
Tuesday, February 10, 2004

It's possible to use XSLT as a code generation tool, even though it's ugly.  (I can't speak to Ruby as an alternative.)  Some starting points:

"Code generation using XSLT"
(Registration required -- bah -- but a very good article.)

"Generate .NET Code With XSLT"

For other examples, Google "XSLT code generation"

One question... do you need to use XSD files specifically as the input?  The IBM article mentions how XSD can be rather cludgy for this purpose because an XSD file is rather complex and noisy -- it's usually better to define a custom schema as the basis for modeling your classes.  See the article for details.

Also, FYI, Visual Studio .Net can generate C# or VB classes directly from an XSD (using the XSD.exe application.)  I'm not aware if there are any comparable Delphi tools.

Robert Jacobson
Tuesday, February 10, 2004

XSL is *not* a 'replacement' for CSS. For some reason, lots of people think it is.

CSS adds styles to text.

XSLT is a (mostly declarative) transformation language. It excels at transforming XML to XML or XHTML or even HTML.

So an XSL which works on an XSD to generate another XSL is a pretty reasonable task. I've done this to do some data conversion.

An XSL to generate, say, Delphi code on the other hand... is possible but will be ugly and difficult. You might have an XSL generate an XML represenation of what you want then use some other tool to convert that to Delphi, but that's convoluted.

There are existing tools which can generate valid XML files given an XSD. I don't know much about them, perhaps some of the above links points to one.

BTW, if you have access to it, Microsoft Office InfoPath is designed to work with XML and XSD and XSL and can be a useful tool.

Tuesday, February 10, 2004

XSLT is actually not bad for generating code. I've written an XSLT stylesheet that will take XML as input and generate java code to build that XML in DOM. Check it out: (do a "view source", it'll look much prettier)

Tuesday, February 10, 2004

If you want to work with the .XSD files directly, the SOM in MSXML4 might be useful. It's really not much different to loading the document up in a DOM, it just gives you access to things more conveniently.

Like others have mentioned, you can use XSLT over the top of it.

If you're using the new fangled Delphi .NET stuff (is it even out?), then you can use the xsd.exe from the .NET sdk. It takes an xsd and can generate .NET classes from it. They'll be in c#, but they're consumable from any .NET language.

Sum Dum Gai
Wednesday, February 11, 2004

*  Recent Topics

*  Fog Creek Home