简体   繁体   English

php xpath查询根据重复子节点中的值获取父节点

[英]php xpath query to get parent node based on value in repeating child nodes

I have an XML file structured as follows: 我有一个XML文件结构如下:

<pictures>
    <picture>
        <title></title>
        <description></description>
        <facts>
            <date></date>
            <place>Unites States</place>
        </facts>
        <people>
            <person>John</person>
            <person>Sue</person>
        </people>
    </picture>
    <picture>
        <title></title>
        <description></description>
        <facts>
            <date></date>
            <place>Canada</place>
        </facts>
        <people>
            <person>Sue</person>
            <person>Jane</person>
        </people>
    </picture>
    <picture>
        <title></title>
        <description></description>
        <facts>
            <date></date>
            <place>Canada</place>
        </facts>
        <people>
            <person>John</person>
            <person>Joe</person>
            <person>Harry</person>
        </people>
    </picture>
<pictures>

In one case, I need to search for pictures where place="Canada". 在一个案例中,我需要搜索place =“Canada”的图片。 I have an XPath that does this fine, as such: 我有一个XPath,这样做很好,因此:

$place = "Canada";
$pics = ($pictures->xpath("//*[place='$place']"));

This pulls the entire "picture" node, so I am able to display title, description, etc. I have another need to find all pictures where person = $person. 这会拉动整个“图片”节点,因此我可以显示标题,描述等。我还有另一个需要找到人= $ person的所有图片。 I use the same type query as above: 我使用与上面相同的类型查询:

$person = "John";
$pics = ($pictures->xpath("//*[person='$person']"));

In this case, the query apparently knows there are 2 pictures with John, but I don't get any of the values for the other nodes. 在这种情况下,查询显然知道John有2张图片,但我没有得到其他节点的任何值。 I'm guessing it has something to do with the repeating child node, but can't figure out how to restructure the XPath to pull all of the picture node for each where I have a match on person. 我猜它与重复的子节点有关,但无法弄清楚如何重构XPath以拉出我在人物上匹配的每一个的所有图片节点。 I tried using attributes instead of values (and modified the query accordingly), but got the same result. 我尝试使用属性而不是值(并相应地修改了查询),但得到了相同的结果。 Can anyone advise what I'm missing here? 谁能告诉我这里缺少什么?

Let's replace the variables first. 让我们先替换变量。 That takes PHP out of the picture. 这使PHP脱离了图片。 The problem is just the proper XPath expression. 问题只是正确的XPath表达式。

//*[place='Canada']

matches any element node that has a child element node place with the text content Canada . 有一个子元素节点的任何元素节点匹配place与文本内容Canada

This is the facts element node - not the picture . 这是facts元素节点 - 而不是picture

Getting the pictures node is slightly different: 获取图片节点略有不同:

//picture[facts/place='Canada']

This would select ANY picture node at ANY DEPTH that matches the condition. 这将选择与条件匹配的任何深度的任何图像节点。

picture[facts/place='Canada']

Would return the same result with the provided XML, but is more specific and matches only picture element nodes that are children of the document element. 将使用提供的XML返回相同的结果,但更具体,并且仅匹配作为文档元素的子元素的picture元素节点。

Now validating the people node is about the same: 现在验证people节点大致相同:

picture[people/person="John"]

You can even combine the two conditions: 你甚至可以结合这两个条件:

picture[facts/place="Canada" and people/person="John"]

Here is a small demo: 这是一个小型演示:

$element = new SimpleXMLElement($xml);

$expressions = [
  '//*[place="Canada"]',
  '//picture[facts/place="Canada"]',
  'picture[facts/place="Canada"]',
  'picture[people/person="John"]',
  'picture[facts/place="Canada" and people/person="John"]',
];

foreach ($expressions as $expression) {
  echo $expression, "\n", str_repeat('-', 60), "\n";
  foreach ($element->xpath($expression) as $index => $found) {
     echo '#', $index, "\n", $found->asXml(), "\n";
  }
  echo "\n";
}

HINT: Your using dyamic values in you XPath expressions. 提示:您在XPath表达式中使用dyamic值。 String literals in XPath 1.0 do not support any kind of escaping. XPath 1.0中的字符串文字不支持任何类型的转义。 A quote in the variable can break you expression. 变量中的引号可以打破你的表达。 See this answer . 看到这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM