首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Xpath在XML中进行搜索

使用Xpath在XML中进行搜索
EN

Stack Overflow用户
提问于 2013-12-09 20:11:26
回答 1查看 145关注 0票数 0

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

代码语言:javascript
运行
复制
$category = $_GET['category'];

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

我的xml结构如下。

代码语言:javascript
运行
复制
<?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是第一个类别,那么它将返回它。我希望它应该返回所有有类别原则的东西。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-09 20:23:53

编辑:

这将返回带array $results的所有<program>-nodes和<category>' 'Doctrine

代码语言:javascript
运行
复制
$results = $xml->xpath("//program[categories/category = 'Doctrine']");

看到它的作用:http://codepad.viper-7.com/yuhgyg

这将返回array $results和所有id<program><category>' 'Doctrine

代码语言:javascript
运行
复制
$results = $xml->xpath("//categories[category = 'Doctrine']/../@id");

看到它的作用:http://codepad.viper-7.com/xhX727

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20479772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档