Google
 

Saturday, March 03, 2007

XML Learning Notes2

Defining and Validating XML with Schemas
  • A way exists to express and validate data designs expressed in XML. This is done through a standard descriptive format referred to as XML schemas (sometimes abbreviated XSD).
  1. About Document Type Definitions (DTDs)
    • The first technology used for validating XML structures was known as Document Type Definition (DTD). By linking a DTD document to an XML file, you can ensure that the XML document contains valid data types and structure.

    • The problem with DTDs is that they have limitations with respect to the things they can do.One glaring limitation is that DTDs can't define data types of elements that appear in a document.

    • But the most damning implication of using DTDs to validate XML is that DTDs are written using a syntax that is completely removed from that of XML itself; if you want to validate your data using the DTD format, you must ascend a learning curve.

    • The most important benefit of XML schemas is that you can write an XML schema using the XML syntax you presumably already know.

    • Before we proceed, it's worth noting that you're never required to validate the XML documents that you use in your application development; XML documents can live out their whole lives without ever knowing or using an XML schema. However, it's a good idea to validate them for the sake of consistency.
  2. About XML Data-Reduced Schemas
    • The COM-based Microsoft XML implementation that existed before the arrival of the .NET framework used a syntax known as XML Data-Reduced (XDR) schemas.
    • Confusingly, the Microsoft documentation refers to XDR as "XML Schemas," even though that's really a different, albeit related, syntax from that provided by the W3C. 
    • The .NET tools support both the XDR and W3C way of expressing schemas, but Visual Studio .NET follows the W3C schema syntax, so you'll likely see the W3C syntax used in .NET applications more often.

      Listing 10.32 Example of an XDR Schema
      <Schema xmlns="urn:schemas-microsoft-com:xml-data"
              xmlns:dt
      ="urn:schemas-microsoft-com:datatypes">

        
      <ElementType name='TITLE'  content='textOnly' />

        
      <AttributeType name='IDType'  dt:type='integer' />

        
      <ElementType name='AUTHOR'  content='textOnly'>
          
      <attribute type='IDType' />
        
      </ElementType>

        
      <ElementType name='BOOK'  content='mixed'>
          
      <element type = 'TITLE' />
          
      <element type = 'AUTHOR'  />
        
      </ElementType>

      </Schema>


    • A Schema node indicates that this is the start of a schema. The two xmlns attributes refer to external schema documents; the first one is for XML itself, the second one is for the data types defined by the Microsoft data types defined for use in XDR schemas.
    • The ElementType nodes in the schema document form the definition of the nodes that compose the document.
    • The Microsoft reference on XDR schemas is at http://msdn.microsoft.com/xml/reference/schema/start.asp . Another useful Microsoft link is the XML Schema Developer's Guide, located at http://msdn.microsoft.com/xml/xmlguide/schema-overview.asp .

    • Note, again, that when Microsoft refers to "XML Schema," it may be referring to either XDR schemas or W3C-style XML schemas. In general, what you get in the .NET tools are true W3C XML schemas.

    • Known as the XML Schema Definition Tool (xsd.exe), this command-line tool can also create basic schemas from existing XML files and build ADO.NET classes in Visual Basic.NET or C# from existing schemas.

  3. Creating W3C XML Schemas
    • A W3C XML schema is conceptually similar to an XDR schema, but has a number of implementation differences.
    • Because XML schema is on its way to becoming an Internet standard, it's better to use the W3C standard format because you can expect a better level of interoperability as the standard propagates.

      <?xml version="1.0"?>
      <xsd:schema xmlns:xsd=" http://www.w3.org/2001/XMLSchema">

      <!-- Your schema definition goes here -->

      </xsd:schema>
      <!--
      Like XDR schemas, W3C schemas are generally linked to external files that provide the basic definition of what a schema is.
      As a result, the W3C-compliant schemas you create will typically begin with a reference to the standard W3C schema definition
      (known as the schema of schemas).
      The W3C schema definition gives your schema access to basic data types and structures
      you'll need to construct schemas to define and validate your XML documents.

      This boilerplate provides the same basic function as the initial definition of the XDR schema shown in a previous example,
      but it provides a different basic schema type and associates it with the xsd namespace.
      This means that you'll often see elements of a W3C schema prefixed with the xsd namespace;
      this is done to prevent namespace collisions
      between elements defined by the xsd schema definition and elements in your documents with the same name.
      -->

  4. Understanding Simple and Complex Types
  5. Validating Documents Using W3C Schemas
  6. Using .NET Framework Objects to Validate XML Schemas
  7. Creating XSD Schemas in Visual Studio .NET
  8. Editing Schema-Validated XML Files in Visual Studio .NET
  9. Creating Schemas from Data Sources Using Visual Studio .NET

book:Addison-Wesley - C Sharp Developer's Guide to ASP.NET XML and ADO.NET.chm
--
Happy day, happy life!

No comments: