php - How to solve a naming conflict when specifying a custom prefix using XPath? -
i'm running php 5.4.14, , i'm trying use xpath search inside xml document. can't know namespace prefix used each namespace, define own prefixes using registerxpathnamespace()
.
the problem if register new prefix xpath (e.g. c
) , document using it, xpath query won't use prefix original one.
let me show sample code show behaviour:
<?php $body = <<<eof <multistatus xmlns="dav:" xmlns:vc="urn:ietf:params:xml:ns:carddav" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:c1="http://calendarserver.org/ns/"> <response> <href>/caldav.php/jorge/contacts/</href> <propstat> <prop> <current-user-principal> <href>/caldav.php/jorge/</href> </current-user-principal> <resourcetype> <collection/> <vc:addressbook/> </resourcetype> <displayname/> <vc:addressbook-home-set> <href>/caldav.php/jorge/</href> </vc:addressbook-home-set> </prop> <status>http/1.1 200 ok</status> </propstat> </response> </multistatus> eof; $xml = new simplexmlelement($body); $xml->registerxpathnamespace('c', 'urn:ietf:params:xml:ns:carddav'); $xpresult = $xml->xpath('//c:addressbook-home-set'); var_dump($xpresult);
if run you'll see query returns no results.
surprisingly, if change registered prefix c other prefix isn't defined, such x, query works expected:
$xml->registerxpathnamespace('x', 'urn:ietf:params:xml:ns:carddav'); $xpresult = $xml->xpath('//x:addressbook-home-set');
am doing wrong? given can't know prefixes used in advance, possible set custom prefix , make sure won't conflict other prefixes in document besides using ugly myprogramunusedprefixc prefix?
i know can prefixes , namespaces used in document, i'd have fixed xpath query strings.
fwiw domxpath has problem (checked it).
unless want go theoretical possibility prefix choose might in use, use improbable guess one. example prefix: sjduwiskjdu
Comments
Post a Comment