使用Xpath PHP在XML中搜索

时间:2013-12-09 20:11:26

标签: php xml xpath

您好我在使用simplexml_load_file和xpath语法在XML文件中搜索。我的xpath查询如下。

$category = $_GET['category'];

$nodes = $xml->xpath("//programs/program[contains(categories/category, '{$category}')]");

我的xml结构正在跟随。

<?xml version="1.0" encoding="UTF-8"?>
<programs>
<program id="789">
    <title>Does Prayer Really Change Life&#8217;s Outcomes?</title>
    <subtitle>The purpose of prayer as described in James 5</subtitle>
    <audio-file>http://www.christianquestions.net</audio-file>
    <youtube-id>FZCBuFwgaAg</youtube-id>
    <categories>
        <category>Doctrine</category>
    </categories>
    <open-par>Prayer is an almost universal phenomenon.  People pray to all kinds of “gods” and we pray about all kinds of things.  We use all kinds of tools to help us pray – prayer shawls, wheels, rugs, books and beads, to name a few.  People pray to worship, to ask for help, guidance, peace, patience, strength, courage, and tolerance.  People pray to complain and vent.  People pray to seek revenge, judgment, and self worth.  People pray to dictate, command and get rich.  People pray for whatever people want, see like or wish.  People pray – does it do any good?</open-par>
    <featured-image>http://www.christianquestions.net/wp-content/uploads/2013/11/Capture4.jpg</featured-image>
</program>
<program id="788">
    <title>How Do We Cope With Abortion?</title>
    <subtitle>The Abortion Debate – is the Bible Pro-Choice or Pro-Life?</subtitle>
    <audio-file>http://www.christianquestions.net/en/2013mp3/788_How_do_We_Cope_with_Abortion_11_17_13.mp3</audio-file>
    <youtube-id>IVqMaaiJxcs</youtube-id>
    <categories>
        <category>Social Issues</category>
    </categories>
    <open-par>Freedom is a good thing.  Freedom to chose, freedom to act, freedom of thought – all good things.  Our society touts this right of freedom especially in the case of a woman’s right to choose.  Freedom though, without responsibility – this is NOT a good thing, for it takes the liberty of freedom and applies it with no boundaries – and without boundaries there can only be anarchy.  So, what is the balance between freedom, responsibility and a woman’s right to choose for the Christian?  Does the Bible tell us?</open-par>
    <featured-image>http://www.christianquestions.net/wp-content/uploads/2013/11/Capture2.jpg</featured-image>
</program>
<program id="787">
    <title>What Makes a True Christian Leader (Part II)</title>
    <subtitle>Outward manifestations of a Christian leader</subtitle>
    <audio-file>http://www.christianquestions.net/en/2013mp3/787_What_makes_a_True_Christian_Leader_Pt_2_11_10_13.mp3</audio-file>
    <youtube-id>rnhPGGpA2gc</youtube-id>
    <categories>
        <category>Character</category>
        <category>Doctrine</category>
    </categories>
    <open-par>About a month ago we began talking about leadership – Christian leadership - and how important it is for a Christian community – any community - to have significant leadership.  Without leadership, people tend towards their own way and more than that, without leadership people tend towards a self-centered and incomplete perception of the world around them.  Our last conversation revolved around the internal aspects of leadership – what makes a leader from the inside out – today we talk about what makes a leader from the outside in.</open-par>
    <featured-image>http://www.christianquestions.net/wp-content/uploads/2013/10/872-serve.jpg</featured-image>
</program>
</programs>

我正在搜索类别文字。因此,例如当我搜索Doctrine时,它应该返回id为789和787的记录,因为它们都具有Doctrine类别。但问题是,如果程序项中有多个值,它只会读取第一个类别值。因此,如果它发现Doctrine是第一类,那么它将返回它。我希望它应该返回所有具有Doctrine类别的东西。感谢

1 个答案:

答案 0 :(得分:0)

修改
这将返回array $results所有<program> - 具有<category>' 'Doctrine的节点:

$results = $xml->xpath("//program[categories/category = 'Doctrine']");

看到它有效:http://codepad.viper-7.com/yuhgyg


这将返回array $results id<program><category>' 'Doctrine的对象:

$results = $xml->xpath("//categories[category = 'Doctrine']/../@id");

看到它正常工作:http://codepad.viper-7.com/xhX727