2010年11月25日星期四

Blog Topic 2 - XML Schema ID/IDREF/IDREFS unique

XML Schema
- 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.

[Definition:]  
ID represents the ID attribute type from [XML 1.0 (Second Edition)]. The ·value space· of ID is the set of all strings that ·match· the NCName production in [Namespaces in XML]. The ·lexical space· of ID is the set of all strings that ·match· the NCName production in [Namespaces in XML]. The ·base type· of ID is NCName. ID should be used only on attributes.

ID has the following ·constraining facets·:

•length
•minLength
•maxLength
•pattern
•enumeration
•whiteSpace

[Definition:]  
IDREF represents the IDREF attribute type from [XML 1.0 (Second Edition)]. The ·value space· of IDREF is the set of all strings that ·match· the NCName production in [Namespaces in XML]. The ·lexical space· of IDREF is the set of strings that ·match· the NCName production in [Namespaces in XML]. The ·base type· of IDREF is NCName. IDREF should be used only on attributes.

IDREF has the following ·constraining facets·:

•length
•minLength
•maxLength
•pattern
•enumeration
•whiteSpace

[Definition:]  
IDREFS represents the IDREFS attribute type from [XML 1.0 (Second Edition)]. IDREFS are derived from IDREF. The ·value space· of IDREFS is the set of finite, non-zero-length sequences of IDREFs. The ·lexical space· of IDREFS is the set of space-separated lists of tokens, of which each token is in the ·lexical space· of IDREF. The ·itemType· of IDREFS is IDREF.

IDREFS has the following ·constraining facets·:

•length
•minLength
•maxLength
•enumeration
•whiteSpace
•pattern

xs:unique is a identity-constraint definition validation rule.
If the {identity-constraint category} is unique, then no two members of the ·qualified node set· have ·key-sequences· whose members are pairwise equal, as defined by Equal in [XML Schemas: Datatypes].



See the following example of ID/IDREF:
XSD Schema - Author is referenced by Author ID in Book
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="BookList">
    <xs:complexType>
            <xs:sequence>
                <xs:element name="AuthorList">
                    <xs:complexType>
                        <xs:sequence>
                        <xs:element name="Author" maxOccurs="unbounded">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:extension base="xs:string">
                                        <xs:attribute name="ID" type="xs:ID" use="required"/>
                                    </xs:extension>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
<xs:attribute name="ISBN" type="xs:string" use="required"/>
                                <xs:attribute name="AuthorID" type="xs:IDREF" use="required"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>


XML Data
<?xml version="1.0" encoding="UTF-8"?>
<BookList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="BookList_Schema_1.xsd">
    <AuthorList>
        <Author ID="A01">Steve Krug</Author>
        <Author ID="A02">Brian Proffitt</Author>
        <Author ID="A03">Amy Shuen</Author>
        <Author ID="A03">Kelly Shuen</Author><!-- The value 'A03' of attribute 'ID' on element 'Author' is not valid with respect to its type, 'ID'. -->
    </AuthorList>
    <Book ISBN="978-1435458994" AuthorID="A02">Take Your iPad to Work</Book>
    <Book ISBN="978-0321657299" AuthorID="A01">Rocket Surgery Made Easy: The Do-It-Yourself</Book>
    <Book ISBN="978-0596529963" AuthorID="A03">Web 2.0: A Strategy Guide</Book>
    <Book ISBN="978-0321344755" AuthorID="A01">Don't Make Me Think: A Common Sense Approach to Web Usability</Book>
    <Book ISBN="978-0321344756" AuthorID="A04">XML Cookbook</Book><!-- There is no ID/IDREF binding for IDREF 'A04'. -->
</BookList>

From the XML Data, it shows the Author ‘Steve Krug’ represented by ID ‘A01’ is the author of 2 books. By applying ID and IDREF, the duplication of keeping Author name in different books is removed; it makes the data keeping and updating more efficient. It can be used to model one-to-many relationship. ID acts as the primary key and IDREF acts as the foreign key.

In the example, the ID/IDREF constraint validation on the value confines the value of ID type cannot repeat such as Author ID ‘A03’. The value of IDREF type should be binding to the value of ID type and it is an error on book with Author ID ‘A04’ as there is no Author with ID ‘A04’.

Moreover, by using ID/IDREF type, it gains the advantage in building transformation style sheet. xsl:key serves as a reference to link the ID and IDREF type data.


See the following example of xsl:key:
XSLT Style Sheet – Book attribute AuthorID link to Author ID to show the name
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xd"
    version="1.0">
    <xsl:output method="text" indent="yes"/>
    <xsl:key name="Author_Key" match="Author" use="@ID"/>
    <xsl:template match="/BookList/AuthorList"/>
    <xsl:template match="/BookList/Book">
        Book: <xsl:value-of select="."/>
        ISBN: <xsl:value-of select="@ISBN"/>
        Author: <xsl:value-of select="key('Author_Key', @AuthorID)"/>
       
    </xsl:template>
</xsl:stylesheet>

Sample Output:
        Book: Take Your iPad to Work
        ISBN: 978-1435458994
        Author: Brian Proffitt
   
        Book: Rocket Surgery Made Easy: The Do-It-Yourself
        ISBN: 978-0321657299
        Author: Steve Krug
   
        Book: Web 2.0: A Strategy Guide
        ISBN: 978-0596529963
        Author: Amy Shuen
   
        Book: Don't Make Me Think: A Common Sense Approach to Web Usability
        ISBN: 978-0321344755
        Author: Steve Krug
   


See the following example of ID/IDREFS:
XSD Schema - Authors are referenced by Author ID in Book
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="BookList">
    <xs:complexType>
            <xs:sequence>
                <xs:element name="AuthorList">
                    <xs:complexType>
                        <xs:sequence>
                        <xs:element name="Author" maxOccurs="unbounded">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:extension base="xs:string">
                                        <xs:attribute name="ID" type="xs:ID" use="required"/>
                                    </xs:extension>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
<xs:attribute name="ISBN" type="xs:string" use="required"/>
                                <xs:attribute name="AuthorID" type="xs:IDREFS" use="required"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>


XML Data
<?xml version="1.0" encoding="UTF-8"?>
<BookList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation=" BookList_Schema_2.xsd">
    <AuthorList>
        <Author ID="A01">Steve Krug</Author>
        <Author ID="A02">Brian Proffitt</Author>
        <Author ID="A03">Amy Shuen</Author>   
</AuthorList>
    <Book ISBN="978-1435458994" AuthorID="A02 A01">Take Your iPad to Work</Book>
    <Book ISBN="978-0321657299" AuthorID="A01 A03 A02">Rocket Surgery Made Easy: The Do-It-Yourself</Book>
    <Book ISBN="978-0596529963" AuthorID="A03">Web 2.0: A Strategy Guide</Book>
    <Book ISBN="978-0321344755" AuthorID="A01">Don't Make Me Think: A Common Sense Approach to Web Usability</Book>
</BookList>

This example shows that the book with attribute AuthorID of IDREF type can bind to set of space-separated values from ID type Author ID.
It can be used to represent many-to-many relationship.


See the following example of xs:unique:
XSD Schema - Book ISBN should be unique
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="BookList">
    <xs:complexType>
            <xs:sequence>
                <xs:element name="Book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
<xs:attribute name="ISBN" type="xs:string" use="required"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
    </xs:complexType>
<xs:unique name="ISBNKey">
          <xs:selector xpath="Book"/>
          <xs:field xpath="@ISBN"/>
    </xs:unique>
</xs:element>
</xs:schema>



XML Data
<?xml version="1.0" encoding="UTF-8"?>
<BookList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="BookList_Schema_3.xsd">
    <Book ISBN="978-1435458994">Take Your iPad to Work</Book>
    <Book ISBN="978-0321657299">Rocket Surgery Made Easy: The Do-It-Yourself</Book>
    <Book ISBN="978-0596529963">Web 2.0: A Strategy Guide</Book>
    <Book ISBN="978-0321344755">Don't Make Me Think: A Common Sense Approach to Web Usability</Book>
    <Book ISBN="978-0321344755">XML Cookbook</Book><!-- Duplicate unique value [978-0321344755] declared for identity constraint "ISBNKey" of element "BookList". -->
</BookList>

The XSD Schema constraints the attribute ISBN should be unique, the xs:selector specific the path and xs:field specific the field to be unique.
The Book with ISBN ‘978-0321344755’ is duplicated and violate the unique identity constraint.


In additions, by applying the ID/IDREF with xs:unique, it can model one-to-one relationship.

See the following example of ID/IDREF and xs:unique:
XSD Schema - Author is referenced by Author ID in Book
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="BookList">
    <xs:complexType>
            <xs:sequence>
                <xs:element name="AuthorList">
                    <xs:complexType>
                        <xs:sequence>
                        <xs:element name="Author" maxOccurs="unbounded">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:extension base="xs:string">
                                        <xs:attribute name="ID" type="xs:ID" use="required"/>
                                    </xs:extension>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
<xs:attribute name="ISBN" type="xs:string" use="required"/>
                                <xs:attribute name="AuthorID" type="xs:IDREF" use="required"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
    </xs:complexType>
<xs:unique name="AuthorKey">
          <xs:selector xpath="Book"/>
          <xs:field xpath="@ AuthorID"/>
    </xs:unique>
</xs:element>
</xs:schema>



XML Data
<?xml version="1.0" encoding="UTF-8"?>
<BookList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="BookList_Schema_4. xsd">
    <AuthorList>
        <Author ID="A01">Steve Krug</Author>
        <Author ID="A02">Brian Proffitt</Author>
        <Author ID="A03">Amy Shuen</Author>
    </AuthorList>
    <Book ISBN="978-1435458994" AuthorID="A02">Take Your iPad to Work</Book>
    <Book ISBN="978-0321657299" AuthorID="A01">Rocket Surgery Made Easy: The Do-It-Yourself</Book>
    <Book ISBN="978-0596529963" AuthorID="A03">Web 2.0: A Strategy Guide</Book>
    <Book ISBN="978-0321344755" AuthorID="A01">Don't Make Me Think: A Common Sense Approach to Web Usability</Book><!-- Duplicate unique value [A01] declared for identity constraint "AuthorKey" of element "BookList". -->
</BookList>

This example shows that Author ID under Author List can appear in book attribute AuthorID one and only one. By applying the ID/IDREF and xs:unique, one-to-one relationship can be modelled.

沒有留言:

發佈留言