Extracting Metadata - Using Schema Definitions Such As XSD
(Page 3 of 22 )
Schemas explicitly describe the structure of data. Schemas often describe the structure of XML files. Schemas offer a fallback position for finding metadata sources. You can describe almost any data with an XML schema, and you can generally create these schemas via a tool such as XSD.EXE that’s part of Visual Studio .NET. (See Footnote 2.) This gives you a good route to creating metadata when there isn’t a more obvious route, such as the programmatic interface of the SQL-92 standard.
(Footnote 2: You can access XSD.EXE within Visual Studio by adding a DataSet or XML Schema to your project.)
TIP: Use schemas such as XSD as a metadata source when your data source is an XML file or a database that’s supported by ADO.NET but doesn’t support the SQL-92 standard.
Several styles of schemas are available—including three styles for XML. (See Footnote 3.) The most important of these is XSD. XSDs are generally created via an inference tool that finds patterns within an XML document and expresses these patterns and rules in XSD. You’ll rarely need to create an XSD manually, but you need to know a bit about how XSD files are structured to read and understand them.
(Footnote 3: The three major schemas for describing XML data are External Data Representation (XDR), Document Type Definition (DTD), and XML Schema Definition (XSD).)
TIP: Appendix A’s section on XSD gives a detailed breakdown of an XSD file.
Schemas are useful anytime you need information about a data structure. XSDs are important in validating that the structure of an XML file is correct. Visual Studio also uses them for IntelliSense, autocompletion, and ToolTips. I use them for the data-driven XML editor of the code generation harness you’ll see in Chapter 3.
NOTE: You’ll see XSD schemas used two different ways in this book. XSD can itself be a metadata source. It can also be used to describe other XML files, including the individual metadata files and the scripting XML used in Chapter 3. Appendix A covers XSD basics. Later in this chapter you’ll see more details about working with XSD as a metadata source. |
Because all ADO.NET data can be expressed as XML, all ADO.NET data can be described by an XSD. This isn’t particularly profound because the XSD specification is sufficiently flexible to describe just about anything. Anything you perceive as data is likely to fit in XML that you can describe with XSD. XSD is itself XML, allowing it to be manipulated with any tool that supports XML.
Directly using XSD as metadata turns out to be really ugly. XSD describes tables and columns in terms of complex types and simple types. The result is that the XPath to access the tables in an XSD looks like this:
<xsl:apply-templates select= "xs:schema/xs:element/xs:complexType/xs:choice/xs:element"/>
NOTE: This is XSLT usage, but the same XPath expression (the quoted part of the previous XSLT fragment) would also be used in a SelectNodes expression in brute-force or CodeDOM generation. |
I don’t know about you, but I’m more likely to look at that line and go “Huh?” than think, “Okay, looping through tables here.” Translating this to a friendly XML metadata format first allows you to write and maintain code templates that use simpler XPath expressions, which are immediately comprehensible, such as the following:
<xsl:apply-templates select="Tables/Table"/>
Because the conversion translates the XML of the XSD schema to the XML of the friendly format, XSLT is a good tool for the conversion. You can find an XSDToFriendlyXML.xslt file in the Downloads section of the Apress Web site (http://www.apress.com); it’s discussed in Appendix B. This chapter’s “Understanding XSD’s Roles in Code Generation” section further discusses creating friendly metadata.
TIP Use metadata that’s friendly to code generation with as much preprocessing, such as data type translations, as possible.
This is from Code Generation in Microsoft .NET, by Kathleen Dollard (Apress, ISBN 1590591372). Check it out at your favorite bookstore today. Buy this book now. |
Next: Extracting Metadata from Databases Such As SQL Server >>
More Database Articles
More By Apress Publishing