dongyan1936 2019-04-18 17:12
浏览 434
已采纳

使用Xpath进行部分匹配

I'm trying to create a search function allowing partial matching by song title or genre using Xpath.

This is my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<playlist>
  <item>
    <songid>USAT29902236</songid>
    <songtitle>I Say a Little Prayer</songtitle>
    <artist>Aretha Franklin</artist>
    <genre>Soul</genre>
    <link>https://www.amazon.com/I-Say-a-Little-Prayer/dp/B001BZD6KO</link>
    <releaseyear>1968</releaseyear>
  </item>
  <item>
    <songid>GBAAM8300001</songid>
    <songtitle>Every Breath You Take</songtitle>
    <artist>The Police</artist>
    <genre>Pop/Rock</genre>
    <link>https://www.amazon.com/Every-Breath-You-Take-Police/dp/B000008JI6</link>
    <releaseyear>1983</releaseyear>
  </item>
  <item>
    <songid>GBBBN7902002</songid>
    <songtitle>London Calling</songtitle>
    <artist>The Clash</artist>
    <genre>Post-punk</genre>
    <link>https://www.amazon.com/London-Calling-Remastered/dp/B00EQRJNTM</link>
    <releaseyear>1979</releaseyear>
  </item>
</playlist>

and this is my search function so far:

function searchSong($words){
    global $xml;

    if(!empty($words)){
        foreach($words as $word){
            //$query = "//playlist/item[contains(songtitle/genre, '{$word}')]";
            $query = "//playlist/item[(songtitle[contains('{$word}')]) and (genre[contains('{$word}')])]";
            $result = $xml->xpath($query);
        }
    }

    print_r($result);
}

Calling the function searchSong(array("take", "soul")) should return the second and first song from XML file, but the array is always empty.

  • 写回答

2条回答 默认 最新

  • dpp34603 2019-04-18 17:33
    关注

    A few errors here: use of and instead of or, assuming searches are case-insensitive, and passing incorrect number of parameters to contains. The last would have triggered PHP warnings if you were looking for them. Also, you're only ever returning the last item you search for.

    Case insensitive searches in XPath 1.0 (which is all PHP supports) are a huge pain to do:

    $result = $xml->query(
        "//playlist/item[(songtitle[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{$word}')]) or (genre[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{$word}')])]"
    );
    

    This assumes you've taken your search terms and converted them to lower-case already. For example:

    <?php
    
    function searchSong($xpath, ...$words)
    {
        $return = [];
        foreach($words as $word) {
            $word = strtolower($word);
            $q = "//playlist/item[(songtitle[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{$word}')]) or (genre[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{$word}')])]";
            $result = $xpath->query($q);
            foreach($result as $node) {
                $return[] = $node;
            }
        }
        return $return;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果