如何使用XML中的XPATH搜索带有撇号引号的字符串?

时间:2013-11-11 05:13:53

标签: php xml xpath

我有以下XML

$xml = <<<XML
<books>
 <book>
  <title>PHP Basic's</title>
  <author>Jim Smith</author>
  <author>Jane Smith</author>
 </book>
 <book>
  <title>PHP Secrets</title>
  <author>Jenny Smythe</author>
 </book>
 <book>
  <title>XML basics</title>
  <author>Joe Black</author>
 </book>
</books>
XML;

遵循PHP代码

$xml = simplexml_load_string($xml);
$books = $xml->xpath("//book[contains(title,'Basic's')]");

问题是:如何搜索带撇号的字符串?我尝试使用like尝试用Single(')和Double(“)引用整个XPATH表达式,但无法创建正确的XPATH表达式。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可以使用'切换"并转义Basic's中的撇号:

$books = $xml->xpath('//book[contains(title,"Basic\'s")]');

Codepad Example