Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed


Xsd.exe Workarounds for Complex Documents
Microsoft's Xsd.exe is a versatile tool for generating XML schema, .NET classes, and DataSets. Apply these Heimlich maneuvers when Xsd.exe chokes on large, complex XML documents.
by Roger Jennings

Posted July 29, 2002

The taxonomy of XML developers contains two species: document-oriented (XDO) developers and object-oriented (XOO) developers. XDO developers traditionally start the XML application development process by designing an XML document and a Document Type Definition (DTD) to describe the document's structure. When your only validation tool is a DTD, everything looks like text; DTDs—by themselves—don't offer the strong data typing expected by database developers. For the most part, XDO folks come from the SGML/XML publishing field and believe that XML—like its SGML superset—should be limited to lexical types and ordinary text as element values.

XOO developers commonly begin with an object hierarchy to describe their documents' contents, structures, and datatypes. They use XML Schema Definition (XSD) language to translate the object hierarchy to a document structure definition with strongly typed element values. Most XOO types have an RDBMS/XML orientation and view all XML documents as Infosets (see Resources). XDO developers often refer to XOO developers as "DB-wonks" or the like. An early June 2002 version of the "Guidelines for the Use of XML within IETF Protocols" IETF draft, which proposed XSD as the preferred schema language, generated heated responses from XML purists in XML-related newsgroups and weblogs. The current IETF draft omits the XSD preference.

The .NET Framework's Xsd.exe command-line application, which I discussed briefly in my "Validate SOAP Messages with XSD Schema" article, lets XDO adherents quickly compare the merits of DTDs and schemas. Xsd.exe generates XSD schemas from XML documents, classes and DataSets from schemas, and schemas from class DLLs or EXEs. My earlier article and most Xsd.exe examples in VS.NET's online help and MSDN articles use small documents with simple structures. Xsd.exe handles trivial examples with aplomb; it takes real-world documents to challenge Xsd.exe's capabilities. I'll relate my experiences—both good and bad—using Xsd.exe to create schemas for large, hierarchical XML text and business documents. I'll also show you the workarounds for bugs and unexpected results I encountered, and point out some of the pitfalls of Xsd.exe's automated schema and class-generation features.

 
Figure 1. Validate an XML Document Against Its Schema

Auto-generating XSD schema files and attempting to validate representative source documents against the schema identifies document design deficiencies. If you can't validate test-case XML documents against an XSD schema, consider changing your documents' structure. You can't take advantage of Xsd.exe's class and DataSet generation features without a valid schema. On the other hand, a defective schema is worse than no schema at all. Spelunking document structures is a waste of time when the validation problem resides in the XSD file.

Reverse the Validation Roles
This article's sample Visual Basic .NET Windows form test harness, XSDGen.sln, (see Figure 1) applies the XmlValidatingReader class in a reverse role: validating schemas against documents instead of validating documents against schemas. I used Xsd.exe to generate schemas for validating three classes of text-based XML documents: U.S. Code of Federal Regulations sections (Section.xsd), tables of contents (Contents.xsd), and full-text search results (Search.xsd).

Section and tables of contents documents contain mixed-content elements, as defined by the XML 1.0 specification's section 3.2.3 and restricted as to sequence and number of elements by XML Schema Part 0: Primer's section 2.5.2. Mixed content isn't common, especially among XDO types, but it's legal. A fourth, strongly typed schema (Invoice.xsd) validates a 448-kB Invoices.xml document for validation performance testing. The test-case documents and schemas are in the sample app's \XSDGen\bin folder.

Back to top











Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home