本文介绍了根据以前的兄弟值,DOMXPath得到同级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个:

  c>  c  $ b 

  // foo [bar =AAA] / baz 

如果你不熟悉XPath表达式,为添加书签。


Let's say I have this:

<foo>
    <bar>CCC</bar>
    <baz>sometexthere</baz>
</foo>
<foo>
    <bar>AAA</bar>
    <baz>sometext</baz>
</foo>
<foo>
    <bar>DDD</bar>
    <baz>something</baz>
</foo>

Now, I want to get the baz value, which is coming right after bar with the value AAA (!but only with the value AAA!). I don't know how many "foo"s I have, so I can't exactly write something like:

$element->item(0) // I don't know the exact number

So how can I get the value of the baz, which follows after bar with a particular value?

(For the example above, I would like to get sometext because it comes after AAA)

解决方案

A basic query would be to find the bars that contain AAA, then navigate to the corresponding baz element.

//foo/bar[.="AAA"]/../baz

Or, find all baz and filter based on the associated bar.

//foo/baz[../bar = "AAA"]

Or, bazs within foos containing bar having AAA.

//foo[bar="AAA"]/baz

If you're not familiar with XPath expressions, bookmark XML Path Language (XPath) for later.

这篇关于根据以前的兄弟值,DOMXPath得到同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 08:36
查看更多