Jump to content

XML - Managing Data Exchange/XLink

From Wikibooks, open books for an open world



Previous Chapter Next Chapter
XPath CSS



Learning objectives

  • Learn different techniques of implementing XLink's in XML
  • create a custom XLink
  • learn the functionality behind various XLink parameters

sponsored by:

The University of Georgia

Terry College of Business

Department of Management Information Systems



Introduction

[edit | edit source]

Through the use of Uniform Resource Identifiers (URI's), an XLink allows elements to be inserted into XML documents that create links between resources such as documents, images, files and other pages. An XLink is similar in concept to an HTML hyperlink, but is more powerful and flexible.

This chapter will be a general overview of the XLink syntax. It will also provide exposure to some of XLink's basic concepts. For the full XLink specification, see the latest version of the standard at:

http://www.w3.org/TR/xlink

[edit | edit source]

XLinks create a linking relationship between two or more resources. They allow for any XML element, image, text or markup files to be specified in the link.

By using a method similar to the centralized formatting of XSL stylesheets, XLinks allow a document's hyperlinks to be isolated and centralized in a separate document. As a linked document's addresses changes, the XLink remains functional.

The use of XLink requires the declaration of the XLink namespace. This namespace provides the global attributes for type, href, role, arcrole, title, show, actuate, label, from and to. The following example would make the prefix xlink available within the tourGuide element.

<tourGuide
  xmlns:xlink="http://www.w3.org/1999/xlink">
  ...
</tourGuide>
[edit | edit source]

The following table outlines the attributes that can be used with the xlink namespace. The global attributes are type, href, role, arcrole, title, show, actuate, label, from, and to. The table also includes descriptions of how the attributes can be used.


Exhibit 1: Table of global attributes

Attributes

Description and Valid Values

type

Describes the meaning of an item

  • simple - basic format similar to html linkage
  • extended - more complex than simple types with multi functional format
  • resource - provides local resources
  • locator - provides remote resources
  • arc - provides the ability to traverse from one resource to another
  • title - readable name or explanation of link

href

Location of resource

  • value is URI

role

Description of XLink's content

  • value is URI
  • describes the element whose role it is

arcrole

Description of XLink's content

  • value is URI
  • describe the relationship between the two sides of the arc

title

Name displayed, usually short description of link

show

Describes behavior of the browser once the XLink has been actuated and loaded

  • new - load in a new window or frame
  • replace - load in same window or frame
  • embed - replace the current item
  • other - look for information elsewhere
  • none - not specified

actuate

Specifies when resource is retrieved or link processing occurs

  • onRequest - when user requests the link
  • onLoad - when page is loaded
  • other - looks for information elsewhere
  • none - not specified

label, from & to

Specifies link direction



XML schema

[edit | edit source]


The following XML schema defines a tour guide that contains at least one city. Each city contains one or more attractions. The name of each attraction is an XLink.

Exhibit 2: XML schema for TourGuide

<?xml version="1.0" encoding="UTF-8"?>
<!--
      Document   : TourGuide.xsd
      Created on : February 28, 2006
      Author     : Billy Timmins
-->
<!--
      Declaration of usage of xlink Namespace
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"
            xmlns:xlink="http://www.w3.org/1999/xlink">  
    <xsd:element name="tourGuide">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="city" type="cityDetails" minOccurs="1" maxOccurs="unbounded" />
            </xsd:sequence>
        </xsd:complexType>
     </xsd:element>
     <!--
     This section will contain the City details
     -->
     <xsd:complexType name="cityDetails">
         <xsd:sequence>
           <xsd:element name="cityName" type="xsd:string"/>
           <xsd:element name="adminUnit" type="xsd:string"/>
           <xsd:element name="country" type="xsd:string"/>
           <xsd:element name="continent">
                <xsd:simpleType>
                  <xsd:restriction base="xsd:string">
                  <xsd:enumeration value="Asia"/>
                  <xsd:enumeration value="Africa"/>
                  <xsd:enumeration value="Australia"/>
                  <xsd:enumeration value="Europe"/>
                  <xsd:enumeration value="North America"/>
                  <xsd:enumeration value="South America"/>
                  <xsd:enumeration value="Antarctica"/>
                  </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="population" type="xsd:integer"/>
            <xsd:element name="description" type="xsd:string"/>
            <xsd:element name="attraction" type="attractionDetails" minOccurs="1" maxOccurs="unbounded"/>
         </xsd:sequence>
     </xsd:complexType>
     <xsd:complexType name="attractionDetails">
         <xsd:sequence>
         <!--   
         Note use of xlink
         -->
            <xsd:element name="attractionName" xlink:type="simple"/>
            <xsd:element name="attractionDescription" type="xsd:string"/>
            <xsd:element name="attractionRating" type="xsd:integer"/>
        </xsd:sequence>
     </xsd:complexType>
</xsd:schema>

XML document

[edit | edit source]


The following XML document shows how the XLink, attractionName, defined in the XML schema, is used in an XML document. Note that it is necessary to include xlink:href="" within the attribute tags in order to define the linked website.

Exhibit 3: XML document for TourGuide.xsd (using XLink)

<?xml version="1.0" encoding="UTF-8"?>
<!--
      Document   : SomeTourGuide.xml
      Created on : February 28, 2006
      Author     : Billy Timmins
-->
<!--
      Declaration of usage of XLink Namespace
-->
<?xml-stylesheet href="TourGuide.xsl" type="text/xsl"?>
<tourGuide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xlink="http://www.w3.org/1999/xlink" xsi:noNamespaceSchemaLocation="TourGuide.xsd">
    <city>
        <cityName>Atlanta</cityName>
        <adminUnit>Georgia</adminUnit>
        <country>USA</country>
        <continent>North America</continent>
        <population>425000</population>
        <description>Atlanta is the capital of and largest city in the U.S. state of Georgia.</description>
        <attraction>
            <!--
            Declaration of XLink and associated link
            -->
            <attractionName xlink:href="http://www.georgiaaquarium.org/"> Georgia Aquarium </attractionName>
            <attractionDescription>World’s Largest Aquarium</attractionDescription>
            <attractionRating>5</attractionRating>
        </attraction>
        <attraction>
            <!--
            Declaration of XLink and associated link
            -->
            <attractionName xlink:href="http://www.high.org/"> High Museum of Art </attractionName>
            <attractionDescription>The High Museum of Art, founded in 1905 as the Atlanta Art Association, is the leading art museum in the Southeastern United States.</attractionDescription>
            <attractionRating>4</attractionRating>
        </attraction>
        <attraction>
            <!--
            Declaration of XLink and associated link
            -->
            <attractionName xlink:href="http://www.underground-atlanta.com/"> Underground Atlanta </attractionName>
            <attractionDescription> Go beneath the streets of a bustling downtown, to the heart of a great American city.  Underground Atlanta is at the center of it all.</attractionDescription>
            <attractionRating>2</attractionRating>
        </attraction>        
    </city>
    <city>
        <cityName>Tampa</cityName>
        <adminUnit>Florida</adminUnit>
        <country>USA</country>
        <continent>North America</continent>
        <population>303000</population>
        <description>Tampa is a major United States city located in Hillsborough County, on the west coast of Florida.</description>
        <attraction>
            <!--
            Declaration of XLink and associated link
            -->
            <attractionName xlink:href="http://www.buschgardens.com/buschgardens/fla/default.aspx"> Bush Gardens </attractionName>
            <attractionDescription>The nation's fourth largest zoo, Bush Gardens is where you can see African animals roaming free and an exciting amusement park featuring its world-famous rides like Kumba and the new inverted roller-coaster, Montu.</attractionDescription>
            <attractionRating>5</attractionRating>
        </attraction>
        <attraction>
            <!--
            Declaration of XLink and associated link
            -->
            <attractionName xlink:href="http://www.plantmuseum.com/"> Henry B. Plant Museum </attractionName>
            <attractionDescription>Discover a museum which transports you to turn-of-the-century Florida.</attractionDescription>
            <attractionRating>1</attractionRating>
        </attraction>      
    </city>
</tourGuide>

XML stylesheet

[edit | edit source]


The following XML stylesheet displays the contents of the XML document.

Exhibit 4: XML stylesheet TourGuide

<?xml version="1.0" encoding="UTF-8"?>
<!--
      Document   : TourGuide.xsl
      Created on : February 28, 2006
      Author     : Billy Timmins
-->
<!--
      Declaration of usage of XLink Namespace
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="xlink" version="1.0">
    <xsl:output method="html"/>
    <!--
    Attribute XLink defined as an href of simple type
    -->
    <xsl:template match="*[@xlink:type = 'simple' and @xlink:href]">
        <a href="{@xlink:href}">
            <xsl:apply-templates/>
        </a>
    </xsl:template>
    <xsl:template match="/">
        <html>
            <head>
                <title>Tour Guide XLink Example</title>
            </head>
            <body>
                <h2>Cities</h2>
                <xsl:apply-templates select="tourGuide"/>
            </body>
        </html>
    </xsl:template>
    <!-- 
    template for handling a link 
    -->
    <xsl:template match="attractionName">
        <a href="{@xlink:href}">
            <xsl:value-of select="."/>
        </a>
    </xsl:template>
    <xsl:template match="tourGuide">
        <table border="1" width="100%">
            <xsl:for-each select="city">
                <tr>
                    <td>
                        <br/>
                        <xsl:text>City: </xsl:text>
                        <xsl:value-of select="cityName"/>
                        <br/>
                        <xsl:text>County: </xsl:text>
                        <xsl:value-of select="adminUnit"/>
                        <br/>    
                        <xsl:text>Continent: </xsl:text>
                        <xsl:value-of select="continent"/>
                        <br/>
                        <xsl:text>Population: </xsl:text>
                        <xsl:value-of select="population"/>
                        <br/>
                        <xsl:text>Description: </xsl:text>
                        <xsl:value-of select="description"/>
                        <br/>              
                        <br/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <xsl:text>Attraction: </xsl:text>
                    </td>
                    <td>
                        <xsl:text>Attraction Description: </xsl:text>
                    </td>
                    <td>
                        <xsl:text>Attraction Rating: </xsl:text>
                    </td>
                </tr>
                <xsl:for-each select="attraction">
                    <tr>
                        <td>
                            <!--
                            application of the template
                            -->
                            <xsl:apply-templates select="attractionName"/>
                        </td>
                        <td>
                            <xsl:value-of select="attractionDescription"/>
                        </td>
                        <td>
                            <xsl:value-of select="attractionRating"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

Summary

[edit | edit source]
XLink is an extremely versatile specification that standardizes the process for linking to other data sources. Not only does XLink support unidirectional linking similar to an anchor tag in HTML but also can be used to create bidirectional links. Additionally, XLink allows for the linkage from any XML element. This gives great freedom to the developer.