note 83922 modified in function.simplexml-element-xpath by danbrown

From: Date: Fri, 25 Sep 2009 17:18:12 +0000
Subject: note 83922 modified in function.simplexml-element-xpath by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-161081@lists.php.net to get a copy of this message
xpath doesn't seem to be able to handle single quotes embedded in the query itself. For instance, I want to find geo coordinates in an xml file based on country name. xml snippet: <zones> <zone country="Cote d'Ivoire" fullName="Yamoussoukro" geo="6.82,-5.28" id="1050"><url prefix="1001" value="fiji.html" /><url prefix="1002" value="C" /></zone> </zones> The following code does not work: <?php $xml = simplexml_load_file("my.xml"); $result = $xml->xpath("//zone[@country='Cote d\'Ivoire']"); foreach ( $result[0]->attributes() as $key => $val ) { print "<div class='coords'>$key: $val</div>\n"; } ?> I have tried many variations on the embedded single quote (i.e. escape codes) but with no result. W3C offers no explanation either. In addition, there doesn't seem to be any way of embedding wildcards in the attribute value (you can embed wildcards in the attribute name). Otherwise the following might be a reasonable substitute in this context: <?php $result = $xml->xpath("//zone[@country='Cote d*Ivoire']"); ?> --was-- xpath doesn't seem to be able to handle single quotes embedded in the query itself. For instance, I want to find geo coordinates in an xml file based on country name. xml snippet: <zones> <zone country="Cote d'Ivoire" fullName="Yamoussoukro" geo="6.82,-5.28" id="1050"><url prefix="1001" value="fiji.html" /><url prefix="1002" value="C" /></zone> </zones> The following code does not work: <?php $xml = simplexml_load_file("my.xml"); $result = $xml->xpath("//zone[@country='Cote d\'Ivoire']"); foreach ( $result[0]->attributes() as $key => $val ) { print "<div class='coords'>$key: $val</div>\n"; } ?> I have tried many variations on the embedded single quote (i.e. escape codes) but with no result. W3C offers no explanation either. In addition, there doesn't seem to be any way of embedding wildcards in the attribute value (you can embed wildcards in the attribute name). Otherwise the following might be a reasonable substitute in this context: <?php $result = $xml->xpath("//zone[@country='Cote d*Ivoire']"); ?> http://php.net/manual/en/function.simplexml-element-xpath.php

« previous php.notes (#161081) next »