Home arrow XML arrow Introduction to Relations in XML Schema
XML

Introduction to Relations in XML Schema


This is the first article in a series concentrating on implementing relations for designing robust XML schema definitions.

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 15
April 17, 2006
TABLE OF CONTENTS:
  1. · Introduction to Relations in XML Schema
  2. · A composite “Primary Key” in XML Schema
  3. · Combining two different roots or hierarchies in a single XML Schema: the problem
  4. · Combining two different roots or hierarchies in a single XML Schema: the solution
  5. · Defining a simple relation in XML Schema: XML Schema example

print this article
SEARCH DEVARTICLES

Introduction to Relations in XML Schema
(Page 1 of 5 )

If you are new to XML Schema, I strongly suggest you go through my series “Designing your own XML Schema.” It will introduce you to all the necessary concepts for designing XML schemas, even if you are a beginner.

A simple “Primary Key” in XML Schema

What is a Primary Key?  Anyone familiar with database design would understand it right away.  A Primary Key is a special type of constraint (or restriction) which makes sure that all the values are unique and without nulls (you can forget about indexing in XML here).  This topic involves a bit of XPATH during the implementation of a primary key in XML schema. 

For example, all the Employee IDs should never repeat and are compulsory.  How do I enforce this?  Let us look into the following complete schema which does the same.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault=
"unqualified">
      <xs:element name="Employees">
            <xs:annotation>
                  <xs:documentation>Contains All Employee information</xs:documentation>
            </xs:annotation>
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="Employee" maxOccurs="unbounded">
                              <xs:complexType>
                                    <xs:sequence>
                                          <xs:element name="Empno" type="xs:int" />
                                          <xs:element name="Ename" type="xs:string" />
                                          <xs:element name="Sal" type="xs:float" />
                                          <xs:element name="Deptno" type="xs:int" />
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                  </xs:sequence>
            </xs:complexType>
            <xs:key name="PK_Employee_Empno">
                  <xs:selector xpath=".//Employee" />
                  <xs:field xpath="Empno" />
            </xs:key>
      </xs:element>
</xs:schema>

The most important code fragment to concentrate on from the above schema is as follows:

            <xs:key name="PK_Employee_Empno">
                  <xs:selector xpath=".//Employee" />
                  <xs:field xpath="Empno" />
            </xs:key>

Within the above code fragment, you can see the definition of “Key.”  I provided some name (“PK_Employee_Empno”) to identify the key.  I also specified that the element “Empno” (in “Employee” element) is the one which should be applicable for the “Key.”  You can also provide more than one element as part of the “Key,” making it a composite primary key. I examine this kind of key in the next section.


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials