0
votes

Hello I am searching within XML file using simplexml_load_file and xpath syntax. My xpath query is following.

$category = $_GET['category'];

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

and my xml structure is following.

<?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>

I am searching for the category text. So for example when I search for Doctrine it should return record with id 789 and 787 because both have the category Doctrine. But the problem is it only reads the first category value if there are multiple within the program item. So if it finds Doctrine as the fist category then it returns it. I want that it should return all that have the category Doctrine. Thanks

1

1 Answers

0
votes

EDIT:
This will return array $results with all <program>-nodes with the <category>' 'Doctrine:

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

see it working: http://codepad.viper-7.com/yuhgyg


This will return array $results with all ids of <program> with the <category>' 'Doctrine:

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

See it working: http://codepad.viper-7.com/xhX727