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

The Next Level - 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
Now, deriving a new datatype by extending the characteristics of an existing one is just one way of getting the job done. The XML Schema specification also supports one other way of deriving new datatypes: by restricting, or constraining, the characteristics of existing types.

In order to understand this, let's go back to the analogy on the previous page, and consider using the "Jedi" datatype as the base for another datatype: "JediMaster" (according to http://www.starwars.com/databank/organization/thejediorder/index.html, Jedi Masters are "those who have shown exceptional devotion and skill in the Force.")

In other words, a "JediMaster" possesses all the characteristics of a "Jedi"...with one additional constraint: a very high midi-chlorian count. Therefore, it is possible to define a "JediMaster" datatype simply by adding a restriction to the definition of the "Jedi" datatype - as demonstrated below:

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions --> <xsd:complexType name="JediMaster"> <xsd:complexContent> <xsd:restriction base="Jedi"> <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:element name="gender" type="xsd:string"/> <xsd:element name="midichlorian-count" > <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="10000" /> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </xsd:schema>
Want to verify that I'm speaking the truth? Take the XML sample on the previous page, alter it to use the new "JediMaster" datatype, and pass it through an XML validator.

<?xml version="1.0" encoding="UTF-8"?> <gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <character xsi:type="JediMaster"> <name>Luke Skywalker</name> <species>Human</species> <language>Basic</language> <home>Tatooine</home> <gender>Male</gender> <midichlorian-count>9000</midichlorian-count> </character> </gallery>
You should see an error, since the value of the <midiclorian-count> element is less than the defined minimum value in the "JediMaster" datatype.

Now, alter the value of the <midiclorian-count> element in your XML sample, and validate it again.

<?xml version="1.0" encoding="UTF-8"?> <gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <character xsi:type="JediMaster"> <name>Luke Skywalker</name> <species>Human</species> <language>Basic</language> <home>Tatooine</home> <gender>Male</gender> <midichlorian-count>18000</midichlorian-count> </character> </gallery>
This time, you should see no errors - indicating that the restriction you've imposed while deriving the new datatype is in effect.

Note also that when deriving by restriction, it is necessary to repeat the definition of all elements in the derived complex type from my original type. As a result, all elements of type "JediMaster" will also be acceptable as elements of type "Jedi".

 
 
>>> 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: