0
我正在使用XPath的contains功能来查询下面的XML文件的“description”元素。当使用“全新”进行查询时(如下面的xpath表达式代码所示),它将检索XML数据。但是,如果使用“低价全新”,则不会检索到任何内容。显而易见的结论是,包含函数只会检索完全匹配的文本字符串,并且如果单词相互分离(如下面的描述元素),则不起作用。XPath - 查询不完全匹配的文本字符串
XPath是否有一个函数或方法在查询描述元素时使用字符串“低价格全新”来检索以下XML数据?谢谢!将使用
/products/product[contains(description, "brand new") and contains(description, "low price")]
当然,要做到这一点
$xmldoc = simplexml_load_file("products.xml");
$query = $xmldoc->xpath('/products/product[contains(description, "brand new")]');
foreach($query as $Products) {
echo $Products->name . " ";
echo $Products->description . "<br>";
}
<products>
<product type="Electronics">
<name>Desktop</name>
<price>499.99</price>
<store>Best Buy</store>
<description>low price, 17 inch screen, brand new </description>
</product>
</products>
不,不与XPath 1.0功能,但你当然可以使用2'contains()'查询。另外,你可能想使用'normalize-whitespace()'。 – Wrikken