根据以前的兄弟值,DOMXPath得到同级 [英] DOMXPath get sibling depending on previous sibling value
问题描述
< foo>
< bar> CCC< / bar>
< baz> sometexthere< / baz>
< / foo>
< foo>
< bar> AAA< / bar>
< baz> sometext< / baz>
< / foo>
< foo>
< bar> DDD< / bar>
< baz> something< / baz>
< / foo>
现在,我想获得正确的 baz 值在栏之后,值 AAA (!但只有值 AAA !)。我不知道我有多少foo,所以我不能写出如下的东西:
$ element - > item(0)//我不知道确切的数字
那么我怎么能得到具有特定值的bar之后的baz的值?
(对于上面的示例,我想获得 sometext 因为它是在 AAA 之后)
一个基本的查询将是找到 bar
s包含 AAA
,然后导航到相应的 baz
元素。 / p>
// foo / bar [。=AAA] /../ baz
或者,找到所有 baz
并根据相关的栏进行过滤
。
// foo / baz [../ bar =AAA]
或 baz
包含 c> c
// foo [bar =AAA] / baz
如果你不熟悉XPath表达式,为 XML路径语言(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 bar
s 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, baz
s within foo
s containing bar
having AAA
.
//foo[bar="AAA"]/baz
If you're not familiar with XPath expressions, bookmark XML Path Language (XPath) for later.
这篇关于根据以前的兄弟值,DOMXPath得到同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!