2

上周我问了一个关于创建脚本来搜索事件信息的 XML 提要的问题。

使用多个 Xpath 查询来搜索 XML

一旦我写了一些代码,我就会回来的建议结束了这个问题。

我现在已经这样做了,所以我想发布我所做的事情,并询问我是否以正确的方式处理了这个问题。

这是我要搜索的参数:

id - 带回具有匹配 id 属性的特定事件的信息。

q - 一个通用搜索查询,将在事件注释中的任何位置返回匹配项

关键字、场地、出票、开始时间 - 在这些特定元素中查找匹配项

program_list, performer_list - 在这些元素的任何子元素中查找匹配项(每个事件中有多个表演者)

dateFrom, dateTo - 允许进行日期范围搜索。

这是一个示例事件:

  <event id="69399">
    <starttimeunixtime>1349791200</starttimeunixtime>
    <endtimeunixtime>1350507600</endtimeunixtime>
    <starttime>2012-10-09 15:00:00</starttime>
    <endtime>2012-10-17 22:00:00</endtime>
    <formatted_startdate>09 October 2012</formatted_startdate>
    <formatted_enddate>17 October 2012</formatted_enddate>
    <formatted_starttime>3:00pm</formatted_starttime>
    <formatted_endtime>10:00pm</formatted_endtime>
    <venue>Royal Albert Hall</venue>
    <title>A concert of Music</title>
    <performer_list>
      <performer_7>
        <performer>A.N. Other</performer>
        <instrument>Viola</instrument>
      </performer_7>
    </performer_list>
    <program_list>
      <program_8>
        <program>Beethoven</program>
        <program_work>Symphony No. 2</program_work>
      </program_8>
    </program_list>
    <description><![CDATA[<p>This is the description&nbsp;This is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the descriptionThis is the description</p>]]></description>
    <shortDescription><![CDATA[<p>This is the short description&nbsp;This is the short description&nbsp;This is the short description&nbsp;This is the short description&nbsp;This is the short description&nbsp;This is the short description&nbsp;This is the short description&nbsp;This is the short description&nbsp;</p>]]></shortDescription>
    <eventType>Highlights</eventType>
    <keywords>orchestra</keywords>
    <ticketInfo><![CDATA[<p>This is the ticket info&nbsp;</p>]]></ticketInfo>
    <ticketed>YES</ticketed>
  </event>

这是我的脚本:

<?
//Load events XML
$events=simplexml_load_file('events.xml');

//If single event find that info
if(isset($_GET['id'])):
    $id=preg_replace("/[^0-9]/","",$_GET['id']);
    $query='@id="'.$id.'"';
    $outputtype="singleevent";

//Allow for a general search that will search all fields
elseif(isset($_GET['q'])):
    $searchterm=strtolower(preg_replace("/[^A-Za-z0-9\s:-]/","",$_GET['q']));
    $query.='contains(translate(.,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"), "'.$searchterm.'") and ';
    echo $query;
    $query= substr_replace($query, "", -5);


//Otherwise build a query to do the search. 
elseif  (!empty($_GET)):
    foreach($_GET as $key => $value):

        //Remove unwanted characters from userinput
        $searchterm=strtolower(preg_replace("/[^A-Za-z0-9\s:-]/","",$value)); 

        //Do a simple search
        //Xpath doesn't support case insensitive searching, so I'm using the Xpath translate function, which swaps uppercase letters for their lowercase counterpart.
        if($key=='keywords' || $key=='venue' || $key=='ticketed' || $key=='starttime'):         
            $query.='contains(translate('.$key.',"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"), "'.$searchterm.'") and ';
        endif;

        // Do a search of composite fields
        if($key=='program_list' || $key=='performer_list'):         
            $query.='contains(translate('.$key.'/*,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"), "'.$searchterm.'") and ';
        endif;  

        // Allow for a date range query (we might have dateFrom, dateTo or both!)

        if($key=='dateFrom'):
            $query.='endtimeunixtime > '.strtotime($searchterm. "00:00:00").' and ';
        endif;

        if($key=='dateTo'):
            $query.='starttimeunixtime < '.strtotime($searchterm." 23:59:59").' and ';
        endif;          
    endforeach;

    // Remove final 'and' from query

    $query=substr_replace($query, "", -5);

//End of Query Builder
endif;

// If there is no query, then show all events.  

if($query==""):
    $query='*';
endif;

//Query XML
$events=($events->xpath('event['.$query.']'));

//Use $events to generate pages

?>

它似乎按要求工作,但我只是想知道是否有人对替代方法有任何反馈/建议,因为我将来需要用 XML 做很多事情。谢谢!

4

0 回答 0