case insensitive xpath searching in php

后端 未结 3 1042
南笙
南笙 2020-11-29 10:54

I have an xml file like this:




Here\'s          


        
相关标签:
3条回答
  • 2020-11-29 11:04

    Gordon's recommendation to use a PHP function from within XPath will prove more flexible should you choose to use that. However, contrary to his answer, the translate string function is available in XPath 1.0 so that means you can use it; your problem is how.

    First, there is the obvious typo that Charles pointed out in his comment to the question. Then there is the logic of how you're trying to match the text values.


    In word form, you are currently asking, "does the text contain the lowercase form of the keyword?" This is not really what you want to be asking. Instead, ask, "does the lowercase text contain the lowercase keyword?" Translating (pardon the pun) that back into XPath-land would be:

    (Note: truncated alphabets for readability)

    //line[contains(translate(text(),'ABC...Z','abc...z'),'chicago')]
    

    The above lowercases the text contained within the line node then checks that it (the lowercased text) contains the keyword chicago.


    And now for the obligatory code snippet (but really, the above idea is what you really need to take home):

    $xml    = simplexml_load_file($data);
    $search = strtolower($keyword);
    $nodes  = $xml->xpath("//line[contains(translate(text(), 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$search')]");
    
    echo 'Got ' . count($nodes) . ' matches!' . PHP_EOL;
    foreach ($nodes as $node){
       echo $node . PHP_EOL;
    }
    

    Edit after dijon's comment

    Inside the foreach, you could access the line number, chapter number and book name like below.

    Line number -- this is just an attribute on the <line> element which makes accessing it super-easy. There are two ways, with SimpleXML, of accessing it: $node['number'] or $node->attributes()->number (I prefer the former).

    Chapter number -- to get at this, as you rightly said, we need to traverse up the tree. If we were using the DOM classes, we would have a handy $node->parentNode property leading us directly to the <chapter> (since it is the immediate ancestor to our <line>). SimpleXML does not have such a handy property, but we can use a relative XPath query to get it. The parent axis allows us to traverse up the tree.

    Since xpath() returns an array we can cheat and use current() to access the first (and only) item in the array returned from it. Then it is just a matter of accessing the number attribute as above.

    // In the near future we can use: current(...)['number'] but not yet
    $chapter = current($node->xpath('./parent::chapter'))->attributes()->number;
    

    Book name -- the process for this is the same as that of accessing the chapter number. A relative XPath query from the <line> could make use of the ancestor axis like ./ancestor::book (or ./parent:chapter/parent::book). Hopefully you can figure out how to access its name attribute.

    0 讨论(0)
  • 2020-11-29 11:22

    I may have missed something... but here's another approach that is IMHO - simpler. How about using PHP's strtolower() before loading the XML into SimpleXML via simplexml_load_string()?

    IE

    $xml = simplexml_load_string(strtolower(file_get_contents($xml_file_path)));
    $keyword = strtolower($_GET['keyword']); //Make sure you sanitize this!
    $kw = $xml->xpath("//line[contains(text(),'$keyword')]");
    

    This way, you're comparing lowercase::lowercase

    0 讨论(0)
  • 2020-11-29 11:24

    See salathe's answer on how to do it with SimpleXml and translate().

    As an alternative/added option to using XPath functions, you can use any PHP function as of PHP5.3, including self defined, in XPath Expressions when using DOM. I am not sure the same is available in SimpleXml.

    // create a DOMDocument and load your XML string into it
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    
    // create a new Xpath and register PHP functions as XPath functions
    $xPath = new DOMXPath($dom);
    $xPath->registerNamespace("php", "http://php.net/xpath");
    $xPath->registerPHPFunctions();
    
    // Setup the query
    $keyword = 'chicago';
    $q = "//line[php:functionString('stripos', text(), '$keyword')]";
    $nodes = $xPath->query($q);
    
    // Iterate the resulting NodeList
    foreach($nodes as $node) {
        echo $node->nodeValue, PHP_EOL;
    }
    

    This will output

    Here's the first line with Chicago in it.
    Here's a line that says chicagogo
    

    For more details, see @salathes blog entry and the PHP Manual.

    0 讨论(0)
提交回复
热议问题