我试图在命名空间 '//ns1:Location//ns1:District) 中获取元素DistrictName
,但没有任何返回。这是我到目前为止所做的。
foreach($xml1->xpath('//ns1:Venue') as $header){
$result = ($header->xpath('//ns1:Venue//ns1:Location//ns1:District//ns1:DistrictName')); // Should output 'something'.
echo "Local2: " . (string) $result[0]. "</br>";
}
soap_response_xml:
...
<ns1:Venue>
<ns1:Name>Rock</ns1:Name>
<ns1:Contact>
<ns1:Name>Rock</ns1:Name>
</ns1:Contact>
<ns1:Location>
<ns1:District>
<ns2:DistrictId>11</ns2:DistrictId>
<ns2:DistrictName>XXXXXXX</ns2:DistrictName>
</ns1:District>
<ns1:Municipaly>
<ns2:MunicipalityId>1111</ns2:MunicipalityId>
<ns2:MunicipalityName>XXXXXXXXX</ns2:MunicipalityName>
</ns1:Municipaly>
</ns1:Location>
</ns1:Venue>
我做错了什么?
如果您的 XML 在字符串中,最简单的方法可能是删除命名空间:
$string = str_replace(array('ns1:', 'ns2:'), array('', ''), $string);
$xml = new SimpleXMLElement($string);
foreach($xml->xpath('//Venue') as $header){
$result = ($header->xpath('Location/District/DistrictName')); // Should output 'something'.
echo "Local2: " . (string) $result[0]. "</br>";
}
另外:没有必要时不要使用//
。 //
的意思是"后裔"。路径分隔符/