XInclude and XPointer – Advanced XML Part 2

Written by on December 4, 2008 in PHP, XML - 3 Comments

This is the second part Advanced XML and PHP series. If you haven’t read first part you can find it here: Introduction to XPath in PHP. You may be not interested right now but unfortunately topic of todays article (XPointer and XInclude) are extensions of XPath, so it would be very helpful if you would go thru it right now, i assure you that XPath is very interesting technology and knowing it may benefit somewhere in the future.

Ok like i said part two will cover XPointer and XInclude, again i am intentionally not going into details, but just showing basics to get you started or even better interested in this technology.

What is XPointer

XPointer is an extension of XPath, it is used to address certain fragments of XML documents.

#xpointer(/articles/article[@id='1']/title)

This is how sample XPointer using XPath may look like. If you do not know which part of it is a XPath then advice you to read the first article from this series. XPointer also can be used in URI’s to address a part of external document. Here is how URI with XPointer looks like:

http://www.example.com/address-book.xml#xpointer(/articles/article[@id='1']/title)

Actually this is all you need to know about XPointer.

Using XInclude and XPointer together

Let’s pretend that we want to a bit change XML file structure from the last article. We will add two articles and add related articles titles. Related articles are considered the ones with at least one matching tag. Imagine what if we would have hundreds of articles in one XML file, how long would it take to update them manually? I do not even want to think about it so let’s create XML file that will handle finding related articles for us.

 
            php
            xpath
 
            dom
            dodocument
 
            xpointer
            xpath
            xinclude
 
            mysql

This is simple XML file, however note two things. First that in line 2 i added xmlns:xi="http://www.w3.org/2001/XInclude", so we can use xi namespace in the document. Second, look at use of this namespace (xi:include) in line 10. If you are familiar with XPath it should be pretty clear for you what is going on there. We made XPointer to look in the current document for articles with tag <tag> (sic) value “xpath” or “php”, also we excluded current article from the search (ie [@id!=1]). Obviously to find related articles for other articles in the list we need to add there <related> tag and appriorate xi:include line.

Now our XML document is well organized, but in the current form it is actually pretty much useless for the end user. We need to generate XML file in which xi:include‘s will be replaced with actual related articles titles. Fortunately it is very simple thanks to PHP DomDocument.

load('articles.xml');
$doc-&gt;formatOutput = true;
 
$doc-&gt;xinclude();
$doc-&gt;save("articles-xinclude.xml");
?&gt;

After executing this script new file – article-xinclude.xml – will be created with xi:include replaced with real content. Beginning of your XML document should look pretty much like this.

 
            php
            xpath
 
    ...

As you can see one related article was found, and this is what we expected, so XInclude with XPath works fine. Your homework assignment is to find out what href attribute with association with xpointer attribute in xi:include can be used for, for those who know HTML (i guess that means 100% of the audience) assignment is a bit easier.

I will publish solution in the next chapter of Advanced XML articles, which will cover XSLT, very interesting solution, some companies that are using it claim to save gigabytes of bandwith thanks to it – so stay tune for it :) .

About the Author

Greg Winiarski is a freelance PHP and JavaScript programmer. He specializes in web applications and WordPress development.

3 Comments on "XInclude and XPointer – Advanced XML Part 2"

  1. Applebees February 28, 2009 at 12:48 pm ·

    I must say that you provide genuine, quality information. Thanks for this!

    BTW, dpn’t you think your blog needs a better wordpress template?

  2. Greg February 28, 2009 at 5:08 pm ·

    Thanks for the comment Applebees.

    As for my template, well i made it by myself – what’s wrong with this one? :)

  3. hajn March 17, 2011 at 4:46 pm ·

    hey,
    the first link “Introduction to XPath in PHP” doesn’t work, seems there is no “href” attribute at all..other than that, good article, thanks!

Leave a Comment