0
你好,我使用simplexml_load_file和xpath語法在XML文件中搜索。我的xpath查詢如下。使用Xpath在XML中搜索PHP
$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’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的記錄,因爲兩者都有類別原則。但問題是如果程序項目中有多個,它只會讀取第一個類別的值。所以如果它發現教條爲拳頭類別,那麼它會返回它。我希望它應該返回所有具有類別原則的內容。由於
我要回''對象與搜索類別不僅ID匹配的所有孩子的。 –
@devdesign請參閱我的編輯;-) – michi
我通過將查詢更改爲'$ nodes = $ xml-> xpath(「// programs/program [categories/category ='{$ category}']」);'來修復它。謝謝 –