Wednesday, January 18, 2012

10b - XML Schemas

Quick questions:

1. The following passage is to be found in the middle of a particular XML document:

The heavily-used<service xlink:type = "simple" xlink:href ="http://www.thetrams.co.uk/croydon"> Croydon Tramlink </service> provides a cross link to nearby <location>Wimbledon</location>, <location>Addington</location> and <location>Beckenham</location>.

What can you say about how the text Croydon Tramlink will be treated by a browser such as Mozilla Firefox?

A browser such as Mozilla Firefox will create a link of "Croydon Tramlink" linking to the specified URL http://www.thetrams.co.uk/croydon.

2. It’s possible to provide validation for a class of XML document using a Document Type Definition (.dtd) file, or using an XML schema. The DTD approach is easier. Why might you want to use the XML schema approach?

Using an XML schema has it's advantages over using a DTD:
  • XML schema are XML documents : same syntax
  • they provide an Object Oriented approach to defining the format of an XML document
  • many more datatypes are available, not just PCDATA and DDATA, such as integer, byte, string, etc. One can even combine these datatypes to create their own
  • XML schemas allow you to require uniqueness within a scope
  • XML schemas allow you to provide lookups
Longer question:

Here is an XML document:

<?xml version="1.0" encoding="UTF-8"?>
<book isbn="0836217462">
            <title>
                          Being a Dog Is a Full-Time Job
             </title>
            <author>Charles M. Schulz</author>
            <character>
                        <name>Snoopy</name>
                        <friend-of>Peppermint Patty</friend-of>
                        <since>1950-10-04</since>
                        <qualification>extroverted beagle</qualification>
            </character>
            <character>
                        <name>Peppermint Patty</name>
                        <since>1966-08-22</since>
                        <qualification>bold, brash and tomboyish</qualification>
            </character>
</book>

An XML schema is to be constructed, which will validate this document and other similar documents. Make notes on the elements etc that this document contains, and record any significant factors about them.

This is the XSD file to validate the above XML document

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="book">
                        <xs:complexType>
                                    <xs:sequence>
                                                <xs:element name="title" type="xs:string"/>
                                                <xs:element name="author" type="xs:string"/>
                                                <xs:element name="character" maxOccurs="unbounded">
                                                            <xs:complexType>
                                                                        <xs:sequence>
                                                                                    <xs:element name="name" type="xs:string"/>
                                                                                    <xs:element name="friend-of" type="xs:string"
                                                                                    minOccurs="0"/>
                                                                                    <xs:element name="since" type="xs:date"/>
                                                                                    <xs:element name="qualification" type="xs:string"/>
                                                                        </xs:sequence>
                                                            </xs:complexType>
                                                </xs:element>
                                    </xs:sequence>
                                    <!--check that a 10 digit isbn number is passed-->
                                    <xs:attribute name="isbn" type="xs:int" use="required">
                                                <xs:simpleType>
                                                            <xs:restriction base="xs:int">
                                                                        <xs:pattern value="\d{10}"/>
                                                            </xs:restriction>
                                                </xs:simpleType>
                                    </xs:attribute>
                        </xs:complexType>
            </xs:element>
</xs:schema>

1 comment: