Monday, March 5, 2007

XSD validation

I spent some time yesterday faffing around with XML and XSD files. In particular, I wanted to use an XML file to send data between systems. The idea being that I could give a sample XML document and and XSD file to someone and they would know enough to interface into the system.

To start with, creating the initial XML document was a pain in the butt! Using my favourite windows text editor TextPad proved to be tedious, so I use Microsoft's XML Notepad 2007. The UI was a bit clunky but it was nice and simple to use.

So XML created, the next step was to generate the XSD. Again, this proved to be a pain in the butt, particularly checking if the XSD syntax was OK. In the end I used Visual Studio 2005 to edit the XMLSchema a it gives the nice red squiggly line under the bits with errors.

So the final step was to validate the XML against the XSD. You think this would be easy right? Well, no.

The browsers (both Firefox2 and IE7) only performed limited validation. They would validate the tags, but not perform the xsd:restrictions. To get this going I had to edit the XML document I wanted to validate and put the name of the XSD file as an attribute of the first element, and then open the XML document via the browser.

eg. <myfirstelement xsi:noNamespaceSchemaLocation="file://c:/xmlschemas/myschema.xsd" xmlns:xsi=""> ...

Good ole XML Notepad seemed the best option. You can choose View-Schemas and then add in your XSD and it will validate against it. Not as simple as I wanted but it worked.

On the Mac I used EditiX (on a 30 day trial) which seemed pretty good, and included the ability to create an XSD from a chunk of XML which can get you started faster. VS2005 also has this feature.

No comments: