xpath_logoXpath is an usefull language to browse a XML file easily. I will explain you the basics of this tool, with a PHP environment (From PHP 5.2.0). If you want a complete documentation, you can read this article of w3schools.com.

Open a XML file

Let’s make a loop on a XML nodes list :

$xml = new SimpleXMLElement('XML_PATH_FILE', Null, True);  
$nodes = $xml->xpath('YOU XPATH REQUEST');  
foreach($nodes as $node) {  
	$node->getName() // Return node's name
	$node->childName // Return childName's value (Case sensitive !)
}

XPath Requests Syntax

In the previous example, you probably saw a line we need to explain :

$nodes = $xml->xpath('YOU XPATH REQUEST');

We will learn how to write request for Xpath

Expressions

Expression Explaination
 nodename  Every nodes named “nodename”
 /  Attached on document’s root
 //  Everywhere on the document
 . Current node
 ..  Parent node
 @  To select an attribute

Some examples :

  • /root/elements/element : Every nodes named “element” which are child of “elements”, which is a child of “root”
  • //elements/element : Every nodes named “element” which are child of “elements”, everywhere in the docuement.
  • //elements/.. : Select parent node of every “elements” nodes
  • //@myattribute : Select every attributes named “myattribute” everywhere in document

Predicates

Predicate Explaination
 /elements/element[1]  Select first “element” of “elements”
 /elements/element[last()]  Select last “element” of “elements”
 /elements/element[last()-1]  Select penultimate “element” of elements”
 /elements/element[position()<3]  Select the first two “element” of “elements”
 //element[@myattribute]  Select “element” which have an attribute named “myattribute”
 //element[@myattribute=’myvalue’]  Select “element” which have an attribute named “myattribute” which have the value “myvalue”
 /elements/element[subelement>10]   Select “element” which have a “subelement” with a value greater than 10 and child of “elements”
 /elements/element[subelement>10]/subsubelement  Select “subsubelement” of “element” which have a “subelement” with a value greater than 10 and child of “elements”

Search unknown elements

Syntax Explaination
 * Any node
 @* Any attribute
 node() Every nodes

Some examples !

  • /elements/* : Every children of node named “elements”
  • //subelement[@*] : Every attributes of nodes named “subelement” everywhere in the document

You now know the basics of the XPath language which allow you to browse XML files easily. If you want to learn more about this language, you can find an article on the W3C website.

XPath and PHP

Imagine we did our xpath request, and we are in our foreach loop. We have an object in our variable $node, we can manipulate it.

Syntax Explaination
 $node->getName()  Get node’s name (Usefull if your request is like “/elements/*”)
 $node->childName  Get child’s value named “childName” (Case sensitive!)

To realize this new knowledge, nothing better than practice 😉

Browse a XML file with Xpath and PHP
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website.
Ok