This is my home, welcome to my blog. There is no limit to the goodness of life. The more you experience and appreciate the goodness of life.

11/26/2010

What are ID, IDREF and IDREFS simple types in XSD?How is xs:unique used to constrain values? Give examples to explain the usage of these XSD constructs.

Before talking about ID, IDREF and IDREFS, we first look at the data types of XML Schema (as below).
A structural drawing


From this picture, the datatypes in XML Schema are divided into simple types and complex types, and simple types are separated into two groups, built-in types and user-defined types. Likewise, built-in primitive types and deriving types make up the built-in types.

Built-in primitive types have 19 kinds, such as time, date, string, float, decimal and so on. Moreover, derived types are generated by restricting built-in primitive or other built-in derived types. In the most of built-in primitive types, only string type and decimal type derive other types.

But now we only pay attention to derived types from string type, hierarchical chart as follows:

Derived from token type, their definition are same as the duplicate attribute in DTD. The ID, IDREF and IDREFS are used to constrain identity.

ID type is used to uniquely identify the elements of XML document. ID attribute value of specific element must unique in whole file.

IDREF and IDREFS attribute values must have the same restrictions as the ID type, and have the same value with ID attribute values. In other words, IDREF and IDREFS attribute values are not point to ID attribute values which do not exist. The difference is IDREFS is allowed to have multiple values which are separated by spaces. So in IDREFS type, space is legitimate.

Meanwhile, XML Schema provides three categories of identity-constraint definitions: xs:unique, xs:key and xs:keyref. The <xsd:unique> element enables us to designate a particular part of an instance document as having to contain a value, whether in the content of an element or an attribute, that is unique. But the XML 1.0 ID type is limited in scope to attribute values only. A unique value in XSD Schema can start with a number and can be of any datatype, and ID type values are not permitted to start with a number or contain space characters. Then, the value of <xs:unique> can be unique within some specified part of the document and need not be unique document-wide. But the value of ID type has to be unique across a whole document.

Let us to see an example about <xs:unique>, and the first is an XML document.

<? Xml version=’1.0’?>
<BookList>
   <Book BookID=”001”>
        <Title>HTTP Developer’s Handbook</Title>
<Author>Chris Shiflett </Author>
<ISBN>0-672-32454-7</ISBN>
</Book>
<Book BookID="002">
                   <Title> Digital Image Processing</Title>
                   <Author>Rafael C. Gonzalez </Author>
                   <ISBN>978-7-121-10207-3</ISBN>
</Book>
</BookList>

In this XML document, the BookID tag is unique.
Then, the XML schema corresponding to this XML file shows as follows:

<?xml version=”1.0”>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”>

<xs: element name=”BookList” type=”BookListType”>
<xs: unique name=”uniqueBookID”>
<xs: selector xpath=”Book”/ >
<xs: field xpath=”@BookID”/>
</xs:unique>
</element>

<xs: complexType name=”BookListType”>
  <xs: sequence>
   <xs: element name=”Book” type=”BookType”>
</xs: sequence>
</xs:complexType>

<xs:complexType name=”BookType”>
 <xs:sequence>
   <xs: element name=”Title” type=”xs: string”/>
<xs: element name=”Author” type=”xs: string”/>
<xs: element name=”ISBN” type=”xs: token”/>
</xs: sequence>
</xs:complexType>

</xs: schema>

As we can see from these codes, the scope of application of the <xs: unique> element is defined by the subelements <xs: selector> and <xs: field> which possess an xpath attribute. The value of the xpath attribute of the <xs: selector> element implies that <Book> element applies the <xs: unique> element. And the value of the xpath attribute of the <xs: field> element is BookID which is the focus of the <xs: unique> element.

The ID and IDREF/IDREFS datatypes are restricted in the extent to which they can represent relational database structures, but their relationship relates to an entire file. The <xs: key> or <xs: keyref> relationship applies xpath to locate paths, thus allowing a processor to focus on accessing only correlative parts of a document.
Finally, let’s take a look at the <xs: key> and <xs: keyref> elements. And the XML file as below:

<?xml version="1.0" encoding="UTF-8"?>
<Library>  
 <BookList>
   <Book BookID=”001”>
        <Title>HTTP Developer’s Handbook</Title>
<Author>Chris Shiflett </Author>
<ISBN>0-672-32454-7</ISBN>
</Book>
<Book BookID="002">
                   <Title> Digital Image Processing</Title>
                   <Author>Rafael C. Gonzalez </Author>
                   <ISBN>978-7-121-10207-3</ISBN>
</Book>
<Book BookID="003">
                   <Title> XML Schema Essential</Title>
                   <Author>R. Allen Wyke </Author>
                   <ISBN>0-471-41259-7</ISBN>
</Book>

</BookList>

<StudentList>
         <Student>
                   <StudentID>01</StudentID>
                   <Name>Jane</Name>
                   <Class>MSC(CS)</Class>
                   <BorrowNumber BookID=
001>1</BorrowNumber>
                   <BorrowNumber BookID=
003>1</BorrowNumber>
         </Student>
    <Student>
                   <StudentID>02</StudentID>
                   <Name>Tom</Name>
                   <Class>CA</Class>
                   <BorrowNumber BookID=
002>1</BorrowNumber>
         </Student>
</StudentList>
</Library>

The XML schema corresponding to this XML file shows as follows:

<?xml version=”1.0”>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”>

<xs: element name=”Library” type=”LibraryType”>
<xs: complexType>
<xs: sequence>
<xs: element name=”BookList” type=”BookListType”>
<xs: element name=”Student” type=”StudentType”>
</xs: sequence>
</xs:complexType>

<xs:keyref name=”BookIDRef” refer=”BookIDKey”>
 <xs: selector xpath=”StudentList/Student/BorrowNumber”/>
<xs: field xpath=”@BookID”>
</xs:keyref>

<xs: key name=”BookIDKey”>
<xs: selector xpath=”BookList/Book”/>
<xs: field xpath=”@BookID”/>
   </xs: key>
</xs:element>

<xs: complexType name=”BookListType”>
  <xs: sequence>
   <xs: element name=”Book” type=”BookType”>
</xs: sequence>
<xs: attribute name=”BookID” type=”xs: string”/>
</xs:complexType>
<xs:complexType name=”BookType”>
 <xs:sequence>
   <xs: element name=”Title” type=”xs: string”/>
<xs: element name=”Author” type=”xs: string”/>
<xs: element name=”ISBN” type=”xs: token”/>
</xs: sequence>
</xs:complexType>

<xs:complexType name=”StudentType”>
 <xs:sequence>
   <xs: element name=”StudentID” type=”xs: string”/>
<xs: element name=”Name” type=”xs: string”/>
<xs: element name=”Class” type=”xs: string”/>
<xs: element name=”BorrowNumber” type=”BorrowNumberType”/>
</xs: sequence>
</xs:complexType>

<xs: complexType name=”BorrowNumberType”>
  <xs: simpleContent>
   <xs: extension base=”xs: integer”>
   <xs: attribute name=”BookID” typy=”xs: string”/>
   </xs: extension>
</xs: simpleContent>
</xs: complexType>

</xs: schema>

In this XML Schema, the <xs: keyref> element also contains an <xs:selector> element and an <xs: field> element. The xpath attributes of those elements help us to locate in the <StudentList> element, or rather the BookID attribute of this element. And it possesses a name and a refer, name is defined as the name of xs: keyref as well as the refer is the name of <xs: key> element. Thereby the value in the refer attribute of the <xs:keyref> element must match the value of a name attribute of an <xs: key> element.

0 评论:

发表评论