XML
  Home arrow XML arrow Page 5 - Doing More With XML Schemas (part 3)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Doing More With XML Schemas (part 3)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2003-01-30

    Table of Contents:
  • Doing More With XML Schemas (part 3)
  • A Day At The Supermarket
  • Of Fruits And Vegetables
  • Taking On The Fleet
  • Breaking The Mold
  • Two For One

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Doing More With XML Schemas (part 3) - Breaking The Mold


    (Page 5 of 6 )

    Next, how about introducing a referential integrity constraint similar to the one in the example above? Let's say we have a master list of available droid types, and only those droid types may be requisitioned for the various ships in the fleet. Here's my new XML document:

    <?xml version="1.0" encoding="UTF-8"?> <fleet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ship name="Jabitha"> <communication> <droid type="C3PO">4</droid> </communication> <repairs> <droid type="R2D2">6</droid> <droid type="LE-BO2D9">1</droid> </repairs> <administration> <droid type="CZ-3">1</droid> </administration> </ship> <ship name="Millennium Falcon"> <communication> <droid type="C3PO">1</droid> </communication> <repairs> <droid type="R2D2">1</droid> <droid type="LE-BO2D9">4</droid> </repairs> <administration> <droid type="CZ-3">5</droid> </administration> </ship> <droids> <droid type="R2D2">astromech droid</droid> <droid type="C3PO">protocol droid</droid> <droid type="CZ-3">humanoid droid</droid> <droid type="LE-BO2D9">repair droid</droid> </droids> </fleet>
    There are two significant changes in this version of the XML document. First, I have introduced a listing of droids which will act as a master list for all the droids on the ships of the fleet. To make things easier, I have further classified the droids on each ship into sections like administration, communication and repairs, based on their advertised functionality.

    Now, all I need to do is update the schema to reflect this referential integrity constraint, in a manner similar to that in the previous example:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="fleet"> <xsd:complexType> <xsd:sequence> <xsd:element name="ship" type="shipType" maxOccurs="unbounded"/> <xsd:element name="droids" type="droidsGroupType"/> </xsd:sequence> </xsd:complexType> <xsd:unique name="NoRedefines"> <xsd:selector xpath="ship"/> <xsd:field xpath="@name"/> </xsd:unique> <xsd:key name="droidNameKey"> <xsd:selector xpath="droids/droid"/> <xsd:field xpath="@type"/> </xsd:key> </xsd:element> <xsd:complexType name="droidType"> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="type" type="xsd:string"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name="shipType"> <xsd:sequence> <xsd:element name="communication" type="droidsGroupType" maxOccurs="unbounded"> <xsd:keyref name="NoFakesCommunication" refer="droidNameKey"> <xsd:selector xpath="droid"/> <xsd:field xpath="@type"/> </xsd:keyref> </xsd:element> <xsd:element name="repairs" type="droidsGroupType" maxOccurs="unbounded"> <xsd:keyref name="NoFakesRepair" refer="droidNameKey"> <xsd:selector xpath="droid"/> <xsd:field xpath="@type"/> </xsd:keyref> </xsd:element> <xsd:element name="administration" type="droidsGroupType" maxOccurs="unbounded"> <xsd:keyref name="NoFakesAdministration" refer="droidNameKey"> <xsd:selector xpath="droid"/> <xsd:field xpath="@type"/> </xsd:keyref> </xsd:element> </xsd:sequence> <xsd:attribute name="name" type="xsd:string"/> </xsd:complexType> <xsd:complexType name="droidsGroupType"> <xsd:sequence> <xsd:element name="droid" type="droidType" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
    First, I have to define a key for my master list of droids - the droid code will do very well here.

    <xsd:key name="droidNameKey"> <xsd:selector xpath="droids/droid"/> <xsd:field xpath="@type"/> </xsd:key>
    I can now reference this key within each functional category on each <ship> via the <xsd:keyref> element:

    <xsd:complexType name="shipType"> <xsd:sequence> <xsd:element name="communication" type="droidsGroupType" maxOccurs="unbounded"> <xsd:keyref name="NoFakesCommunication" refer="droidNameKey"> <xsd:selector xpath="droid"/> <xsd:field xpath="@type"/> </xsd:keyref> </xsd:element> <xsd:element name="repairs" type="droidsGroupType" maxOccurs="unbounded"> <xsd:keyref name="NoFakesRepair" refer="droidNameKey"> <xsd:selector xpath="droid"/> <xsd:field xpath="@type"/> </xsd:keyref> </xsd:element> <xsd:element name="administration" type="droidsGroupType" maxOccurs="unbounded"> <xsd:keyref name="NoFakesAdministration" refer="droidNameKey"> <xsd:selector xpath="droid"/> <xsd:field xpath="@type"/> </xsd:keyref> </xsd:element> </xsd:sequence> <xsd:attribute name="name" type="xsd:string"/> </xsd:complexType>
    As I have three functional groups for droids on each ship, the key reference needs to be repeated thrice, one for each group (you could probably do this in a more efficient manner if you have a large number of groups - I leave that to you as an exercise, preferring this slightly clunkier option for illustrative purposes). The "refer" attribute of each <xsd:keyref> element links this constraint to the droid master list via the unique label "droidNameKey".

    And that's it! You now have two constraints in a single schema definition, one ensuring that ship names are unique, and the other ensuring that only valid droids appear in each ship. Try it out and see for yourself!

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


     

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT