Skip to content

PHP XPath Tutorial – Advanced XML Part 1

Few years back when i first read book about XML i could not understand what the hyip was all about, some markup language that allows to store data in well organized way. I also remember parsing XML files in PHP few years back. Whole file was raed using fread() and then programmer had to create some kind of parser to process XML file … completely ridicolous. Sure ActionScript, probably Java and some other languages as well already had classes for parsing XML files, but that was not the case for PHP, so i thought that this is a technology that do not deserve a second look.

Fast Forward to today, i read an article covering interesting XML features like XPath, XPoiner, XInclude nad XSLT. Today, i want to introduce you to one of them – XPath. I guess it’s name reaveals what it is all about. Basically XPath is a language that allows to localize and fetch information from XML tree.

First we need basic XML document, nothing to complicated but nothing to simple either to show the power of XPath.

<?xml version="1.0" encoding="UTF-8"?>
<articles>
    <article id="1">
        <tags>
            <tag>php</tag>
            <tag>xpath</tag>
        </tags>
        <title>PHP XPath Example</title>
    </article>
    <article id="2">
        <tags>
            <tag>dom</tag>
            <tag>dodocument</tag>
        </tags>
        <title>DomDocument Tutorial</title>
    </article>
</articles>

Using XPath with DomDocument Object

Obviously new technologies are introduced to make development process easier not harder, so we will use DomDocument object because it has implemented support for XPath.

load('articles.xml');
 
$xpath = new DOMXPath($doc);
$arts = $xpath->query("/articles/article/title");
 
foreach ($arts as $art)
{
    echo $art->nodeValue."";
}

This example outputs

PHP XPath Example
DomDocument Tutorial

I assume you already know basics of object oriented programming so the most important line here is line 6. Note how path there is given. It starts with sign “/” it means that we start searching from the root of the document. We can also use more complex paths for example:

/articles/article[@id='1']/title

Using @ sign means that we want ‘id’ attribute equal to 1. If you replace path in PHP example with this path, then as result you will get

PHP XPath Example

But that’s not all XPath allows for even more complicated searches, replace path in PHP scripts with the following path.

/articles/article/tags[tag='php' or tag='xpath']/../title

Do you know what will we get in the response? Analyze our XML document, before reading further.

As a result we get on the screen

PHP XPath Example

First query is looking in the tags section if it contains tag “tag” with value equal to “php” or “xpath” then it goes “one level up” (eg ‘..’) and fetches article title. Queries can be very complicated and contain a lot of conditions and even “if” statements however my goal was to introduce you to XPath not explain everything, mainly because i am beginner as well, but more over it requires a whole book to cover all possibilities that XPath gives.

If after this article you want more (and i hope you do) then visit XPath Documentation at W3C. I also have to mention Lucas – XPath, XPointer, XInclude article, if not him i would
never now about this stuff.

Published inGeneral