Monday, July 4, 2011

X-Link Extended Links

What is X-Link: X-Link is a specification that allows to link between different XML tags. It is very much like an anchor in HTML, you know the regular <a href="..."></a>. However, X-Link is much more versatile and powerful as we will see.

Unlike HTML anchor tag (which is defined to be able to display a visible link), the use of X-Link is not that limited, and depends on where it is used (Is not all other XML based specifications). As such X-Link specification is not designed to be used on its own, there must be other specifications that define specific rules to use it. It is pretty much like XML itself. XML can be used in many ways, and other standards and specifications define what some XML content means. SOAP and WSDL are two specification that use XML specification. Similarly, Extensible Business Reporting Language is a specification that uses X-Link.


Types of X-Link: There are two types of X-Links - Simple and Extended. As the name of this article suggests, we are not going to concentrate too much on simple links. Plus, there are so many of them available, one wonderful tutorial is available here.


What is an id of an element: The id of an element is not the value of an attribute called 'id'. Instead, the id is the value of an attribute of type xsd:ID, here xsd is the prefix bound to the namespace "http://www.w3.org/2001/XMLSchema" (the schema namespace). Some schema validators like Xerces ensures that the ID is unique in a single XML document. So it is not possible to get an element's id without having access to its schema. Now the question is, can a single element have two different ids? the answer is yes, if the element has two different attributes of type xsd:ID.





How to create a link: Believe it or not, any XML element can become an X-Link. It just needs to have a few attributes from the X-Link namespace (which is "http://www.w3.org/1999/xlink"). So, the following is an example of a simple link.


<guide xmlns="..." xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="myxml.xml#atag" />


and here is myxml.xml


<a xmlns="..." > <x id="atag" /> </a>



The xlink:type attribute is mandatory for all xlink tags except simple links. In simple links, if xlink:href is provided, xlink:type is not required. It tells the processor what kind of xlink tag it is.


So, as simple as that. Its no wonder that these are called simple links.


Extended Links: The extended links are extremely powerful, and a little obscure to understand for a beginner. A simple link connects one tag with another, or it may connect a tag with a whole document. It always has a starting point (the xlink tag itself) and an ending point. However, an extended link has multiple starting and ending points.


An extended link must have the value of xlink:type attribute equal to "extended", so that an xlink processor can know it is an extended link. The extended link can have one or more child elements. The child elements can be a locator, a resource or an arc. A locator is a pointer to a remote resource. A resource is a (well as the name says) resource itself. An arc is something that connects locators and resources. An arc has starting and ending points. Here is a sample XML document, which we will use for demonstration. We will call it pc.xml


<a>
    <p id="p1">Parent</p>
    <p id="p2">Child</p>
    <p id="p3">Child2</p>
    <p id="p4">Parent2</p>
    <p id="p5">Parent3</p>
    <p id="p6">Child3</p>
    <p id="p7">Child4</p>
</a>


Now let us consider this following extended link




<l xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns="..." xlink:type="extended">
    <loc xlink:type="locator" xlink:href="pc.xml#p1" label="p"/>
    <loc xlink:type="locator" xlink:href="pc.xml#p2" label="c"/>
    <loc xlink:type="locator" xlink:href="pc.xml#p3" label="c"/>
    <arc xlink:type="arc" xlink:from="p" xlink:to="c" />
</l>


It creates the following arcs.




How does it work: There is a lot going on in here. Let us look at it step by step. There are three locators and an arc in the link. As with any X-Link tag, the arc and locator tag can have any name. The xlink:type="locator" attribute value is what makes a locator a locator. For arcs, xlink:type must be "arc". The locator has an href. The value of an href attribute is a URL. In case we need to point to a node inside a document, we can use an X-Pointer expression. Here, all the locators point to certain elements in pc.xml. The arc has xlink:from and xlink:to set to values equal to the locators' xlink:label attributes. Note that there are two locator having the same label 'c'. This is why two arcs are actually created with only one arc element.


What is XPointer: XPointer is a specification that allows pointing to a specific node of an XML. In our case, we are using the format [filename]#[nodeId]. If we omit the filename (thus only #[nodeId]), it would mean that the node (in our case the element) is in the same file as the XLink. X-Pointer in itself is very rich and will require an article on its own. At this stage, we will only stick to the id of the element.


We could also use separate labels in the locators and then have two arc elements to do the same job. Lets now make something more complex.



<l xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns="..." xlink:type="extended">
    <loc xlink:type="locator" xlink:href="pc.xml#p1" label="p"/>
    <loc xlink:type="locator" xlink:href="pc.xml#p2" label="c"/>
    <loc xlink:type="locator" xlink:href="pc.xml#p3" label="c"/>
    <loc xlink:type="locator" xlink:href="pc.xml#p4" label="p1"/>
    <loc xlink:type="locator" xlink:href="pc.xml#p5" label="p2"/>
    <loc xlink:type="locator" xlink:href="pc.xml#p6" label="c2"/>
    <loc xlink:type="locator" xlink:href="pc.xml#p7" label="c2"/>
    <arc xlink:type="arc" xlink:from="p" xlink:to="c" />
    <arc xlink:type="arc" xlink:from="p" xlink:to="p1" />
    <arc xlink:type="arc" xlink:from="p" xlink:to="p2" />
    <arc xlink:type="arc" xlink:from="p2" xlink:to="c2" />
</l>



And this is what it creates



Wow!, now we have a tree structure. We can also create a complete graph if we choose to do so.

Local Resources: A locator is a pointer to a remote resource. Now say for example, I want to provide some description to the element p1. One way would be to create an element with a description and have a locator point to it.



<l xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns="..." xlink:type="extended">
    <loc xlink:type="locator" xlink:href="pc.xml#p1" label="p"/>
    <loc xlink:type="locator" xlink:href="#desc" label="c"/>   
    <arc xlink:type="arc" xlink:from="p" xlink:to="c" />
    <desc id="desc">Description ... blah ... blah ...</desc>
</l>


But, X-Link specification allows a cleaner way to do that with less number of elements, with xlink:type="resource". A resource is like a locator, except for the fact that it is the resource instead of a pointer to a remote resource. The following shows an example.


<l xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns="..." xlink:type="extended">
    <loc xlink:type="locator" xlink:href="pc.xml#p1" label="p"/>
    <desc xlink:type="resource" label="c">Description ... blah blah ...
    </desc>   
    <arc xlink:type="arc" xlink:from="p" xlink:to="c" />
</l>


The role, arcrole, title Attributes (Semantic Attributes): Now, if a specification/application is using multiple kinds of links, how does it distinguish between different types of links? What I mean is, how does the application/specification know about the purpose of a link? One approach would be to define specific names for the tags that are used in the links. Another way would be to use a xlink:role attribute. xlink:role attribute can be used in a link (both simple and extended), a locator or a resource. The value of the xlink:role attribute is a URI. An arc on the other hand, can instead have an arcrole. The purpose of both role and arcrole attributes are to specify the purpose of the X-Link link, arc, locator or resource.

A title attribute is a human readable description and is applicable to all of them. A long title can become a sub-element itself. The following is an example.



<l xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns="..." xlink:type="extended"
xlink:role="http://debasishwebguide.blogspot.com/sampleLink"
xlink:title="A sample extended link">
    <loc xlink:type="locator" xlink:href="pc.xml#p1" label="p"
    xlink:role="http://debasishwebguide.blogspot.com/elemLoc" 
    xlink:title="Locates the p1 element"/>
    
    <desc xlink:type="resource" label="c"
    xlink:role="http://debasishwebguide.blogspot.com/description" 
    xlink:title="Description">Description ... blah blah ...
    </desc>   
    
    <arc xlink:type="arc" xlink:from="p" xlink:to="c" 
    xlink:arcrole="http://debasishwebguide.blogspot.com/elementDescription">
        <t xlink:type="title">
            A very long title
        </t>
    </arc>
</l>




The show and actuate Attributes (Behavior Attributes): The show and actuate attributes are specifically meant for applications that interact with humans. These are applicable only to arcs and simple links.

The show attribute can take the values "new", "replace", "embed", "other", and "none". It is intended to specify how to show the target resource when the link/arc is activated.

The actuate attribute can take the values "onLoad", "onRequest", "other", and "none". It is intended to be used to specify when to activate the link/arc.

Reference: XML Linking Language (XLink) Version 1.1

1 comments:

Namrata said...

well explained!!!

Post a Comment