【问题标题】:Xpath partial matching search of an RSS feed [duplicate]RSS提要的Xpath部分匹配搜索[重复]
【发布时间】:2015-04-23 08:56:59
【问题描述】:

我有一个网页设置,它使用简单的 XML 来检索 RSS 提要,然后将其回显到页面上。我正在尝试设置一个搜索功能,允许用户输入对 RSS 提要的查询,然后它将在页面上显示结果。

我知道你可以使用 Xpath contains

/item[contains(title, 'query')]/description 

但是,我希望将其中的“查询”部分替换为在搜索框中键入的任何内容。

我们将不胜感激任何帮助或指向相关网站的链接以及答案。

提前致谢

【问题讨论】:

    标签: php xml xpath rss


    【解决方案1】:

    注意:我认为您正在寻求解决方案的应用程序语言是 PHP:

    您需要按以下方式动态放置搜索关键字:

    //item[contains(title,'".$searchKeyword."')]/description

    下面是例子:

    <?php
    $xml = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <area>
            <photographer_id>1</photographer_id>
            <photographer>John</photographer>
            <image>a</image>
        </area>
        <area>
            <photographer_id>1</photographer_id>
            <photographer>John</photographer>
            <image>b</image>
        </area>
        <area>
            <photographer_id>1</photographer_id>
            <photographer>John</photographer>
            <image>c</image>
        </area>
        <area>
            <photographer_id>2</photographer_id>
            <photographer>Fred</photographer>
            <image>a</image>
        </area>
    </root>
    XML;
    
    $sxe = new SimpleXMLElement($xml);
    $searchKeyword = "Fred";
    $area = $sxe->xpath("//area[contains(photographer,'".$searchKeyword."')]");
    
    print_r($area);
    

    【讨论】:

    • 谢谢,这对我来说更清楚了。 $searchKeyword 变量将输入搜索框中的内容,所以我会在搜索框所在的表单上使用 post 并将其存储为要在 $area 中使用的变量吗?
    • 是的,你没看错。搜索关键字将包含来自搜索框的发布值
    • Daniel 如果您觉得此答案有帮助。投票吧。这将是一个道德破坏者。谢谢!
    • 另外,由于将从 URL 中检索要使用的 XML,我需要将 标记之间的所有内容替换为: $rss = simplexml_load_file('url.asp');
    • 是的,您将不得不...我提到的 xml 就是例如。如果它来自任何网址,那么您将不得不使用您编写的内容,即 $rss = simplexml_load_file('url.asp'); , 如果它将以字符串形式出现,则 $rss = simplexml_load_string($xmlstring);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 2017-05-08
    • 2011-10-22
    • 2017-08-12
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    相关资源
    最近更新 更多