XML
  Home arrow XML arrow Page 3 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Doing More With XML Schemas (part 3) - Of Fruits And Vegetables


    (Page 3 of 6 )

    The schema design on the previous page ensures that a truant store manager cannot damage the integrity of my XML document instance by throwing up new aisles wherever (s)he likes. Now, let's take it one step further and add another integrity check, this one to ensure that the <item>s in each aisle actually exist in the store's inventory system.

    Here's the updated document instance - note that, this time around, I've added an extra <items> block that serves as the inventory, matching item codes with a human-readable description of each item.

    <?xml version="1.0" encoding="UTF-8"?> <supermarket name="MyMart" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <aisle name="fruits" number="1"> <item code="001" quantity="230" price="1.00"/> <item code="002" quantity="121" price="2.45"/> <item code="003" quantity="60" price="3.15"/> </aisle> <aisle name="vegetables" number="2"> <item code="004" quantity="500" price="1.15"/> <item code="005" quantity="600" price="0.75"/> </aisle> <items> <item code="001">oranges</item> <item code="002">apples</item> <item code="003">pineapples</item> <item code="004">onions</item> <item code="005">potatoes</item> </items> </supermarket>
    Only items that exist in the inventory "database" should appear within the various <aisle>s of the supermarket.

    In order to enforce this rule, I need to update my schema design again.

    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="supermarket"> <xsd:complexType> <xsd:sequence> <xsd:element name="aisle" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="item" maxOccurs="unbounded"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="code" type="xsd:positiveInteger"/> <xsd:attribute name="quantity" type="xsd:positiveInteger"/> <xsd:attribute name="price" type="xsd:decimal"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name="name" type="xsd:string"/> <xsd:attribute name="number" type="xsd:positiveInteger"/> </xsd:complexType>

    <xsd:keyref name="NoIllegalEntries" refer="itemKey"> <xsd:selector xpath="item"/> <xsd:field xpath="@code"/> </xsd:keyref>

    </xsd:element> <xsd:element name="items"> <xsd:complexType> <xsd:sequence> <xsd:element name="item" maxOccurs="unbounded"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="code" type="xsd:positiveInteger"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name="name" type="xsd:string"/> </xsd:complexType>

    <xsd:key name="itemKey"> <xsd:selector xpath="items/item"/> <xsd:field xpath="@code"/> </xsd:key>

    </xsd:element> </xsd:schema>
    In case you missed it, here's the important bit:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- snip --> <xsd:keyref name="NoIllegalEntries" refer="itemKey"> <xsd:selector xpath="item"/> <xsd:field xpath="@code"/> </xsd:keyref> <xsd:key name="itemKey"> <xsd:selector xpath="items/item"/> <xsd:field xpath="@code"/> </xsd:key> <!-- snip --> </xsd:schema>
    In order to enforce the "only-add-those-items-to-aisles-that-exist-in-inventory" rule, I need to import two new concepts into my schema, concepts that may already be familiar to you from your work on relational databases: keys and relationships.

    In the database world, a primary key uniquely identifies every record in a table - it might be a single field, or a combination of fields, but it serves as a unique fingerprint to identify any record in a table. It also serves as an important component of the relational database model - relations between different tables are created on the basis of primary keys.

    Based on this knowledge, it's pretty obvious what the primary key is in the scenario above - it's the "code" attribute of each <item>. Or, to put it in schema lingo,

    <xsd:key name="itemKey"> <xsd:selector xpath="items/item"/> <xsd:field xpath="@code"/> </xsd:key>
    Key definition takes place via the <xsd:key> element, which identifies the key by a unique name. The <xsd:selector> and <xsd:field> elements are then used, in conjunction with regular XPath expressions, to drill down to the element/attribute combination representing the primary key.

    Once the key is defined, the next step is to define a relationship around which it pivots. In the scenario above, it is fairly clear that the key reference has to be maintained between the <item> under the <items> element (the inventory master list) and the <item> under the <aisle> element (the inventory itself).

    With this in mind, let's add a condition to the schema definition with the <xsd:keyref> element.

    <xsd:keyref name="NoIllegalEntries" refer="itemKey"> <xsd:selector xpath="item"/> <xsd:field xpath="@code"/> </xsd:keyref>
    The <xsd:keyref> element is used to indicate a reference to a key defined elsewhere in the schema. I have given the reference an appropriate name - "NoIllegalEntries" - which is displayed to the XML document author by the validator in case a violation of the reference takes place. The "refer" attribute of the <xsd:keyref> element links this reference to the primary key defined previously, via the unique key name "itemKey".

    At this point, an integrity check has been added to the schema to ensure that only valid <items> from the inventory appear in the <aisle>s. You can verify this by adding an item to the aisles with a product code not listed in the inventory - your XML validator should barf and throw up lots of ugly errors.

    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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway