Skip to main content

SimpleXML and namespaces

By Kevin Yank

Programming

Share:

Free JavaScript Book!

Write powerful, clean and maintainable JavaScript.

RRP $11.95

There’s a lot about SimpleXML, PHP5’s new API for accessing the contents of XML documents, in SitePoint’s recently-published book No Nonsense XML Web Development With PHP, but one thing it doesn’t cover is how to use SimpleXML with a document that makes use of XML Namespaces.

Take this document, for example–a simplified RSS 1.0 feed:


<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel rdf:about="http://www.sitepoint.com/">
    <title>SitePoint.com</title>
    <link>http://www.sitepoint.com/</link>
    <description>SitePoint is the natural place to go to grow your online business.</description>
    <items>
      <rdf:Seq>
        <rdf:li rdf:resource="http://www.sitepoint.com/article/take-command-ajax" />
      </rdf:Seq>
    </items>
  </channel>
  <item rdf:about="http://www.sitepoint.com/article/take-command-ajax">
    <title>Take Command with AJAX</title>
    <link>http://www.sitepoint.com/article/take-command-ajax</link>
    <description>Want to get a bang out of your AJAX artillery?</description>
    <dc:date>2005-10-14T04:00:00Z</dc:date>
  </item>
</rdf:RDF>

In PHP5, here’s how you might think to use SimpleXML’s API to get at the date of every item in the feed:


$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf');
foreach ($feed->item as $item) {
  echo $item->date;
}

But this won’t work, because the date element has a namespace prefix (<dc:date>), so it can’t be accessed by the usual means.

Here’s the solution. First, check what the URI is for the namespace. In this case, the dc: prefix maps to the URI http://purl.org/dc/elements/1.1/:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">

Then use the children method of the SimpleXML object, passing it that URI:


$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf');
foreach ($feed->item as $item) {
  $ns_dc = $item->children('http://purl.org/dc/elements/1.1/');
  echo $ns_dc->date;
}

When you pass the namespace URI to the children method, you get a SimpleXML collection of the child elements belonging to that namespace. You can work with that collection the same way you would with any SimpleXML collection.

You can use the attributes method in the same way to obtain attributes with namespace prefixes.

Kevin Yank is an accomplished web developer, speaker, trainer and author of Build Your Own Database Driven Website Using PHP & MySQL and Co-Author of Simply JavaScript and Everything You Know About CSS is Wrong! Kevin loves to share his wealth of knowledge and it didn't stop at books, he's also the course instructor to 3 online courses in web development. Currently Kevin is the Director of Front End Engineering at Culture Amp.

New books out now!

Learn valuable skills with a practical introduction to Python programming!


Give yourself more options and write higher quality CSS with CSS Optimization Basics.