Latest: buggy behaviour of parent:: in PHP 5.3.3

Content with Style

Web Technique

A quick look at XInclude and XLink

by Pascal Opitz on December 13 2008, 16:47

Part of my current project is to transform thousands of static XML files, all interlinked and some of them referencing others as data subset that I need for the transformation. Time to have a quick look at XInclude and XLink.

XLink

Xlink is an XML application that defines hyperlinks in XML documents. While XLink supports extended links, which are able to define multiple hyperlinks from one XML node, the form that is easy to deal with straight away is the one defined by type="simple".

Like with all XML applications that use certain definitions, don't forget to include the namespaces in the XML and XSL document, and if you generate HTML, exclude them from being outputted by the transformation using the exclude-result-prefixes attribute.
Below is an example for a transformation in XSL.


<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    exclude-result-prefixes="xlink">
    
    <xsl:output method="html"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="*[@xlink:type = 'simple' and @xlink:href]">
        <a href="{@xlink:href}"><xsl:apply-templates/></a>
    </xsl:template>
</xsl:stylesheet>

As for the extended type of XLink references, this could come in handy to generate link lists for certain text elements and similar stuff where a 1:N relationship is needed.

XInclude

XInclude is an inclusion mechanism for XML, think SSI or PHP's include(). Just that it is that bit more powerful, because it can use XPointer to identify the nodes that it needs to include from the referenced document, and it provides a fallback element for when the inclusion fails.


<xi:include href="my.xml#xpointer(//node)"> 
  <xi:fallback>
   <fb>error</fb>
  </xi:fallback>
</xi:include>

Sadly this is not natively supported in browsers, but various DOM parsers have a mechanism in place that will include the referenced files, e.g. PHP's DomDocument->xinclude:


$xml = new DomDocument;
$xml->load('data.xml');
$xml->xinclude();

Again make sure that the namespaces are included. Also, if you use nested levels of includes, depending on the DOM parser, this might not automatically get resolved. For how to address this behaviour in PHP, see the comments at PHP.net.

Comments

Don't miss the opportunity to leave the first comment.

Leave your comment

Comments are moderated.
Tags allowed: a, strong, em, code, ul, ol, li, q, blockquote, br, p

Advertisement
Advertisement