简体   繁体   中英

How to search string with apostrophe quote with XPATH in XML?

I have following 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;

And following PHP Code

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

Question is: How to search for a string with apostrophe quote? I tried with either with like tried to quote entire XPATH expression with Single (') and Double (") quote but could not be able to create proper XPATH expression. Can anyone help me out?

You could switch ' with " and escape the apostrophe in Basic's :

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

Codepad Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM