Home arrow XML arrow Page 5 - Doing More With XML Schemas (part 2)

Speaking In The Abstract - XML

In this second part, find out how to derive new element types by constraining existing ones, control access to your schema definitions, and redefine externally-provided schemas in place.

TABLE OF CONTENTS:
  1. Doing More With XML Schemas (part 2)
  2. Feeling The Force
  3. The Next Level
  4. Big Brother Is Watching...
  5. Speaking In The Abstract
  6. Going Local
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 5
January 23, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
While on the subject of controlling the manner in which type definitions can be used, it's instructive to also look at abstract type definitions. If a base type spawns several new sub-types, as in the example below,

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="gallery"> <xsd:complexType> <xsd:sequence> <xsd:element name="character" type="starWarsEntity" maxOccurs="unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element> <!-- base definition --> <xsd:complexType name="starWarsEntity"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="species" type="xsd:string"/> <xsd:element name="language" type="xsd:string"/> <xsd:element name="home" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <!-- derived definition --> <xsd:complexType name="Ewok"> <xsd:complexContent> <xsd:extension base="starWarsEntity"> <xsd:sequence> <xsd:element name="vehicle" type="xsd:string"/> <xsd:element name="society" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="Human"> <xsd:complexContent> <xsd:extension base="starWarsEntity"> <xsd:sequence> <xsd:element name="gender" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <!-- and so on --> </xsd:schema>
schema authors can force document authors to be more precise in their usage of these types by declaring the base type as abstract.

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- base definition --> <xsd:complexType name="starWarsEntity" abstract="true"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="species" type="xsd:string"/> <xsd:element name="language" type="xsd:string"/> <xsd:element name="home" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
This requires document authors to specifically name the sub-type whenever they use it in a document instance. Failure to do so will result in XML validation errors. For example, while the following XML document instance is certainly conformant to the rules laid down for the base type "starWarsEntity",

<?xml version="1.0" encoding="UTF-8"?> <gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <character> <name>Luke Skywalker</name> <species>Human</species> <language>Basic</language> <home>Tatooine</home> </character> </gallery>
the XML validator will still generate errors while parsing it, as "starWarsEntity" has been defined as an abstract type. It is only when the document author specifies a type via the "type" attribute

<?xml version="1.0" encoding="UTF-8"?> <gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <character xsi:type="Human"> <name>Luke Skywalker</name> <species>Human</species> <language>Basic</language> <home>Tatooine</home> <gender>Male</gender> </character> </gallery>
that validation will take place without errors.

Again, this mechanism assists in reducing the risk of errors, and in controlling the manner in which schema definitions are used by document authors. It's also possible to declare specific elements (rather than types) as abstract - all you need is a substitution group, which you can read about at http://www.w3.org/TR/xmlschema-0/#SubsGroups

 
 
>>> More XML Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: