我想确定术语"B&B Hotel Hamburg Altona"在XML数据中的位置。
XML如下:
<PlaceSearchResponse>
<status>OK</status>
<result>
<name>Hotel Grüner Berg</name>
</result>
<result>
<name>Hotel ABC</name>
</result>
<result>
<name>Hotel Lorem Ipsum</name>
</result>
<result>
<name>B&B Hotel Hamburg-Altona</name>
</result>
<result>
<name>Hotel Lorem Ipsum</name>
</result>
<result>
<name>Hotel Lorem Ipsum</name>
</result>
我的php脚本:
$apiUmgebungUrl = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=Hotel+in+Hamburg&radius=500&sensor=true&key=geheimerkey";
$xml = new SimpleXMLElement($apiUmgebungUrl, null, true);
echo $q = $xml->xpath('result/[contains(name,"B&B Hotel Hamburg-Altona")][position() <= 1]');
但我得到了错误:
警告:SimpleXMLElement::xpath():中的表达式无效C: 第14行上的''axamp_18''htdocs''xmlposition.php
警告:SimpleXMLElement::xpath():xmlXPathEval:中的评估失败C: 第14行上的''axamp_18''htdocs''xmlposition.php
[]
中的条件)不应由/
与元素名称分隔。
result[contains(name,"B&B Hotel Hamburg-Altona")][position() <= 1]
还要注意,您的XML是不正确的:&
应该表示为&
。
要获得位置,请计算前面同级的数量:
count(result[contains(name,"B&B Hotel Hamburg-Altona")][position() <= 1]/preceding-sibling::*)