Pages

Banner 468

Saturday 28 January 2012

CMT3315 Lab 10 - XLink

0 comments
 

Today's post answers questions related to XLink.

Wherever possible, lab questions were reproduced before providing the answer. The lab handout may be downloaded from here.

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?

The ability to store extended links in a "third party" document allows for the creation of relationships between resources independedntly of the resources themselves. In other words, by storing links in a separate file (a linkbase) you would eliminate the need to change the source or target resources which could be expensive or even impossible if you are not the owner of the resource itself. In fact, the ability to link resources over which you have no control is one of the main advantages offered by XLink in general.


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"
  • show="new"
  • show="embed"

The "show" XLink attribute defines where the link should be opened. "Replace" instructs the application to open the ending resource in the same window in which the starting resource was loaded. "New" instructs the application to open the ending resource in a new window. "Embed" instructs the application to load the presentation of the ending resource in place of the presentation of the starting resource. In practice, the ending resource is merged with the starting resource, similar to how the HTML "<img> tag works.

Which of these three attribute values is the default?

The default value for the "show" attribute is "Replace".


Longer Questions

The answers to the longer questions are given below. Please refer to the Lab 10a handout for the questions themselves.

1a. Using xlink-specific attributes, any element in an XML document can be set to act as a hyperlink. To use these attributes we must first declare the Xlink namespace - "http://www.w3.org/1999/xlink" - in the XML document. XML allows for two types of hyperlinks: simple links and extended links. Simple links are one-directional, very similar to hyperlinks in HTML while extended links provide more functionality.

In our example, a specific piece of text - "this website" - found within the "<message>" element needs to be changed into a link pointing to "http://engineering.suite101.com/article.cfm/wind_power". If we were to add XLink attributes to the "<message> element, the entire text within it would be changed into a hyperlink, which is not what we want. We must therefore create a new element to wrap the words "this website" and add the Xlink attributes to this new element. We must also change the DTD to support this new element as well as the Xlink attributes which we are about to add.

Listing 1 below shows the modified XML containing the hyperlink. The XLink namespace is declared at line 4 in the root element. Line 11 shows the new element called "<link>" wrapping the words "this website". The xlink:type="simple" attribute specifies that this element is a simple link while the xlink:href= attribute specifies the URL of the target document or ending resource in XLink terminology.

 
  <?xml version="1.0"?>
  <!DOCTYPE memo SYSTEM "memo.dtd">
  <?xml-stylesheet href="stylesheet02.css" type="text/css"?>
  <memo xmlns:xlink="http://www.w3.org/1999/xlink">
   <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 
    <link xlink:type="simple" xlink:href="http://engineering.suite101.com/article.cfm/wind_power">this website</link>. 
    Tell me what you think. 
   </message>
  </memo>
 
 

Since we added new elements and attributes to our XML document, it will no longer validate against the original DTD. Listing 2 below shows the modified DTD that supports the changes made in the XML above. Line 3 defines the "xmlns:xlink" attribute of the memo element as fixed-value (the XLink namespace). Line 9 changes the "message" element definition to accept 0 or more parsed character data (#PCDATA) values and/or "link" elements. Line 10 defines the new "link" element and specifies that it should contain #PCDATA values. Lines 11 to 13 specify that every "link" element must have an "href" attribute and a "type" attribute, where the latter must be either set to "simple" or "extended". These changes will ensure that the XML document above successfully validates against the DTD.

  <?xml version= "1.0" encoding="UTF-8"?>
  <!ELEMENT memo (heading, date, time, sender, addressee, message)>
  <!ATTLIST memo xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink">
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT date (#PCDATA)>
  <!ELEMENT time (#PCDATA)>
  <!ELEMENT sender (#PCDATA)>
  <!ELEMENT addressee (#PCDATA)>
  <!ELEMENT message (#PCDATA|link)*>
  <!ELEMENT link (#PCDATA)>
  <!ATTLIST link 
  xlink:href CDATA #REQUIRED
  xlink:type (simple|extended)  #REQUIRED>
 

In this example, only two XLink attributes were used: "type" and "href" which are bot required in order to have a valid hyperlink. Other optional XLink attributes include "show" which defines where to open the link and "actuate" which defines when to show the linked resource.

1b. 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?

In order to make the link point directly to the specified point, all we need to do is add a "#" followed by the name of the anchor to the end of our target URL as follows:

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

2. Here is another XML document:

  <?xml version="1.0"?>
  <!DOCTYPE memo SYSTEM "memo.dtd">
  <?xml-stylesheet href="stylesheet02.css" type="text/css"?>
  <memo xmlns:xlink="http://www.w3.org/1999/xlink">
   <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?

Listing 3 below shows the modified XML document. Since all we are doing is replacing the XLink's target URL, we do not need to make any changes to DTD.

 
  <?xml version="1.0"?>
  <!DOCTYPE memo SYSTEM "memo.dtd">
  <?xml-stylesheet href="stylesheet02.css" type="text/css"?>
  <memo xmlns:xlink="http://www.w3.org/1999/xlink">
   <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 
    <link xlink:type="simple" xlink:href="http://vincent-lui.suite101.com/solar-paneling-for-the-home-a226122">this website</link>. 
    Tell me what you think. 
   </message>
  </memo>
 
 

Leave a Reply