Skip to content

Instantly share code, notes, and snippets.

@crilleengvall
Last active December 17, 2015 13:49
Show Gist options
  • Save crilleengvall/5620201 to your computer and use it in GitHub Desktop.
Save crilleengvall/5620201 to your computer and use it in GitHub Desktop.
Default namespaced xml and xpath
<?xml version="1.0"?>
<bookstore xmlns="urn:xmlns:25hoursaday-com:bookstore">
<book>
<title>Lord of the Rings</title>
<author>J.R.R. Tolkien</author>
</book>
</bookstore>
<?xml version="1.0"?>
<bk:bookstore xmlns:bk="urn:xmlns:25hoursaday-com:bookstore">
<bk:book>
<bk:title>Lord of the Rings</bk:title>
<bk:author>J.R.R. Tolkien</bk:author>
</bk:book>
</bk:bookstore>
<?php
$xml = simplexml_load_file('namespaced.xml');
$result = $xml->xpath('/bk:bookstore/bk:book/bk:title');
echo $result[0];
echo "<br />";
$xml = simplexml_load_file('default-namespaced.xml');
$result_from_default_namespace = $xml->xpath("/*[local-name()='bookstore']/*[local-name()='book']/*[local-name()='title']");
echo $result_from_default_namespace[0];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment