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>

Saturday, January 14, 2012

10a - XLink

Quick questions:

1. One of the advantages claimed for the “extended links”, that the W3C consortium intended to be part of the XLink language, was that the definition of a particular hyperlink could be located, not in the local resource (the document where the link starts), or the remote resource (the document where the link ends), but in a quite different “third party” document. Why might this be an advantage?

This allows links to be stored in a separate location: link base. When links need to be changed, the original document need not be amended, but rather, the link in the link base.

2. The XLink language provides an attribute for a hyperlink called show – it has several possible values. What is the effect of providing such a link with each of the following attribute values?

show=”replace” : opens the link in the current presentation format (window etc.), replacing the current content
show=”new” : opens a new tab/window to display the link
show=”embed” : embeds the link in a presentation layer, such as a frame, or part of the current window

Which of these three attribute values is the default?

"replace" is the default attribute value of "show"

Longer questions:

1. Here is an XML document:

<?xml version="1.0"?>
<!DOCTYPE memo SYSTEM "memo.dtd">
<?xml-stylesheet href="stylesheet02.css" type="text/css"?>
<memo>
            <heading>memo 1334</heading>
            <date>date: 11 November 09</date>
            <time>time: 09:30</time>
            <sender>from: The Managing Director</sender>
            <addressee>to: Heads of all Departments</addressee>
  <message>I think we should be making wind-turbines. Have a look at this website. Tell me what you think. </message>
</memo>

The accompanying .dtd file looks like this:

<?xml version= "1.0" ?>
<!DOCTYPE memo [
<!ELEMENT memo (heading, date, time, sender, addressee, message)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT time (#PCDATA)>
<!ELEMENT sender (#PCDATA)>
<!ELEMENT addressee (#PCDATA)>
<!ELEMENT message (#PCDATA)>
]>

At the point where the document says this website, there is supposed to be a hyperlink that takes the reader to the website: http://engineering.suite101.com/article.cfm/wind_power

a) Amend the document, so that the link is in fact there. Make any necessary changes to the .dtd file as well.

The changed XML file

<?xml version="1.0"?>
<!DOCTYPE memo>
<?xml-stylesheet href="stylesheet02.css" type="text/css"?>
<memo>
            <heading>memo 1334</heading>
            <date>date: 11 November 09</date>
            <time>time: 09:30</time>
            <sender>from: The Managing Director</sender>
            <addressee>to: Heads of all Departments</addressee>
            <message>I think we should be making wind-turbines. Have a look at
                        <websiteLink xlink:type="simple" xlink:href="http://engineering.suite101.com/article.cfm/
                        wind_power">this website</websiteLink>.  Tell me what you think.
            </message>
</memo>

The changed DTD file:

<?xml version= "1.0" ?>
<!DOCTYPE memo [
<!ELEMENT memo (heading, date, time, sender, addressee, message)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT time (#PCDATA)>
<!ELEMENT sender (#PCDATA)>
<!ELEMENT addressee (#PCDATA)>
<!ELEMENT message (#PCDATA | websiteLink)*>
<!ELEMENT websiteLink(#PCDATA)>
<!ATTLIST websiteLink
xlink:href CDATA #REQUIRED
xlink:type (simple | extended) "simple">
]>

b) Suppose that the heading of one of the sections in the target website is <A NAME=“WE Elec Facts”>Wind Energy Electricity Facts</A>, including the tags as shown. What changes would you have to make to the link in the managing director’s memo, to make the hyperlink finish at that point rather than at the wind_power document as a whole?

Using XPointer, we can link to a part of the document, rather than to the start of it. Using the # symbol while refering to the name="We Elec Facts" will direct us to that part of the document. the websiteLink tag will need to be amended as follows:

<websiteLink xlink:type="simple" link:href="http://engineering.suite101.com/article.cfm/
wind_power#xpointer(name('WE Elec Facts'))">this website</websiteLink>

2. Here is another XML document:

<?xml version="1.0"?>
<!DOCTYPE memo SYSTEM "memo.dtd">
<?xml-stylesheet href="stylesheet02.css" type="text/css"?>
<memo>
            <heading>memo 1335</heading>
            <date>date: 11 November 09</date>
            <time>time: 09:45</time>
            <sender>from: The Managing Director</sender>
            <addressee>to: Heads of all Departments</addressee>
  <message>I think we should be making solar panels. Have a look at this website. Tell me what you think. </message>
</memo> 

At the point where the document says this website, there is supposed to be a hyperlink that takes the reader to a suitable website. Find one, and amend the document, so that the link is in fact there. Is it necessary to make any changes to the .dtd file, or can we use the file as you amended it before?

We do not need to replace anything in the DTD. Just by specifying another XLink, we will link to another website.