Introduction to Relations in XML Schema - Defining a simple relation in XML Schema: XML Schema example (Page 5 of 5 )
Till now we have gone through defining primary keys and combining hierarchies within a root element. Now it is time to look at the concept of “relation.”
Every developer knows what a “relation” means in an RDBMS. The terms “referential integrity” or “foreign key” are also used for this concept in RDBMS theory. We can even enforce the same types of relations between hierarchies of elements within the same XML schema (or external schemas as well).
Enforcing a relation is quite straightforward in XML schema, when we know how to implement a “primary key.” Let us modify the schema presented in the previous section to enforce a relation between “dept” and “Employee” elements:
The above code fragment says that the “deptno” in “Employee” should “refer” to the values in “PK_Dept_Deptno” (which is nothing but “deptno” in “dept”) for integrity. We also named the relation “FK_deptEmployee.”
How about an XML document for the above XML schema? The next section shows you that.
Defining a simple relation in XML Schema: XML document sample
In the previous section, I defined an XML schema to enforce a simple relation in between two element hierarchies. The following is an XML document sample for the previously defined XML schema.
<?xml version="1.0" encoding="UTF-8"?>
<Organization xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:noNamespaceSchemaLocation="C:Documents and SettingsAdministratorDesktopEmployees.xsd">
The above XML document is error free as the values in the “deptno” element of “Employee” exist in the “deptno” element of “dept.” Try changing “20” (in “Employee”) to “30” (which does not exist in “dept”). The XML document becomes invalid during parsing!
Any comments, suggestions, feedback, bugs, errors are highly appreciated at firstname.lastname@example.org
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.