A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP simplexml: why does xpath stop working? - page 1
User InfoPosts
PHP simplexml: why does xpath stop working?#1
A strange thing happened after a supplier changed the XML header a bit. I used to be able to read stuff using xpath, but now I can't even get a reply with


They changed it from this...

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE NewsML SYSTEM "http://www.newsml.org/dl.php?fn=NewsML/1.2/specification/NewsML_1.2.dtd" [
<!ENTITY % nitf SYSTEM "http://www.nitf.org/IPTC/NITF/3.4/specification/dtd/nitf-3-4.dtd">

to this:

<?xml version="1.0" encoding="iso-8859-1"?>
xsi:schemaLocation="http://iptc.org/std/NewsML/2003-10-10/ http://www.iptc.org/std/NewsML/1.2/specification/NewsML_1.2.xsd http://iptc.org/std/NITF/2006-10-18/ http://contentdienst.pressetext.com/misc/nitf-3-4.xsd"

posted date: 2008-12-08 23:51:00

Re: PHP simplexml: why does xpath stop working?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-08 23:51:01

Re: PHP simplexml: why does xpath stop working?#3
Most likely this is because they've introduced a default namespace (xmlns="http://iptc.org/std/NewsML/2003-10-10/") into their document. SimpleXML's support for default namespaces is not very good, to put it mildly.Can you try to explicitly register a namespace prefix:$xml->registerXPathNamespace("n", "http://iptc.org/std/NewsML/2003-10-10/");$xml->xpath('/n:NewsML');You would have to adapt your XPath expressions to use the "n:" prefix on every element. Here is some additional info: http://people.ischool.berkeley.edu/~felix/xml/php-and-xmlns.html.EDIT: As per the spec: The registerXPathNamespace() function creates a prefix/ns context for the next XPath query.This means it would have to be called before every XPath query, thus a function to wrap XPath queries would be the natural thing to do:function simplexml_xpath_ns($element, $xpath, $xmlns){ foreach ($xmlns as $prefix_uri) { list($prefix, $uri) = explode("=", $prefix_uri, 2); $element->registerXPathNamespace($prefix, $uri); } return $element->xpath($xpath);}Usage:$xmlns = ["n=http://iptc.org/std/NewsML/2003-10-10/"];$result = simplexml_xpath_ns($xml, '/n:NewsML', $xmlns);

posted date: 2008-12-09 00:27:00

Re: PHP simplexml: why does xpath stop working?#4
Thanks a bunch! Also, it seems you have to register the namespaces before every single xpath request.. if you could add this to your answer, it'd be perfect ;)

posted date: 2008-12-09 01:18:00

Re: PHP simplexml: why does xpath stop working?#5
Added, thanks. :)

posted date: 2008-12-09 01:37:00

Re: PHP simplexml: why does xpath stop working?#6
Thanks... that fixed my XPATH queries of my Google Earth (KML) file. Which would only work if I remove the xmlns attribute from my XML file.

posted date: 2015-01-26 14:37:00

Re: PHP simplexml: why does xpath stop working?#7
@Jakobud Because namespaces aren't just attributes, they change the entire element. Think of them as… colors. Thought experiment: <foo> is default (black). <foo xmlns="rgb(255,0,0)"> sets a new default. Now the entire element (and its descendants!) are red. Now, xpath('//foo') will only select black (that's how it works). You must make it recognize red with registerXPathNamespace("red", "rgb(255,0,0)"). Now you can do xpath('//red:foo'). (Note: <foo xmlns:red="rgb(255,0,0)">leaves the element at the default but introduces the prefix red for later use, i.e. <red:bar>.)

posted date: 2015-01-26 22:46:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.