XPATH - 选择具有特定属性的所有子节点

发布于 2024-08-26 00:18:15 字数 415 浏览 4 评论 0 原文

从具有特定属性值的节点开始查找具有特定属性值的所有子节点的 xpath 查询是什么?

这与我之前发布的有关解析 rdf xml 文件的问题有关 - 我以为我已经解决了它,但还没有完全解决。

例如,我试图解析并获取所有 rdf:about 属性值。我这个工作正常。我需要添加以下条件 - 解析需要在找到特定的 rdf:about 值后开始。

我正在 PHP 中工作,并使用 DomDocument 并使用以下 xpath 查询:

$xpath->query('//@rdf:about');

它可以很好地找到所有 rdf:about 属性。

我需要扩展它以仅查找 rdf:about 属性等于某项的节点后面的那些属性。

如何?

What would be the xpath query to find all child nodes with a specific attribute value, but starting from a node with a specific attribute value?

This is kind of related to a question I posted earlier about parsing an rdf xml file - I thought I had solved it but not quite yet.

For example, I am trying to parse and grab all of the rdf:about attribute values. I have this working fine. I need to add the following condition though - parsing needs to start after a specific rdf:about value is found.

I am working in PHP and and using DomDocument and am using the following xpath query:

$xpath->query('//@rdf:about');

It is finding all rdf:about attributes fine.

I need to extend this to only find those attributes that come after the node whose rdf:about attribute is equal to something.

How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-09-02 00:18:15
$xpath->query("//*[@rdf:about='http://rdfs.org/sioc/ns#']/following-sibling::*/@rdf:about");

使用示例脚本,输出以以下内容开头/结尾的 URL:

http://rdfs.org/sioc/ns#Community
http://rdfs.org/sioc/ns#Container
...
http://rdfs.org/sioc/ns#reference
http://rdfs.org/sioc/ns#subject
$xpath->query("//*[@rdf:about='http://rdfs.org/sioc/ns#']/following-sibling::*/@rdf:about");

With your sample script, outputs URLs starting/ending with:

http://rdfs.org/sioc/ns#Community
http://rdfs.org/sioc/ns#Container
...
http://rdfs.org/sioc/ns#reference
http://rdfs.org/sioc/ns#subject
哆啦不做梦 2024-09-02 00:18:15

嗨,克里斯,抱歉,但这对我来说不太有效。如果您运行我的以下代码:

    $dom = new DomDocument;
$dom->load('http://rdfs.org/sioc/ns#');
$xpath = new DOMXPath($dom);

$elements = array();
foreach($xpath->query('//@rdf:about') as $element) {

    array_push($elements, $element->value);
}

foreach($elements as $element) {

    echo("<br />" . $element);
}

您应该获得 sioc 本体中的元素列表。问题是前 9 个节点实际上并不是本体的一部分,所以我想忽略它们并从具有 'http://rdfs.org/sioc/ns#' 作为 rdf:about 属性的值。

通过包含您的 xpath 查询,我什么也没得到。

HI Chris sorry but that isnt quite working for me. If you run my following code:

    $dom = new DomDocument;
$dom->load('http://rdfs.org/sioc/ns#');
$xpath = new DOMXPath($dom);

$elements = array();
foreach($xpath->query('//@rdf:about') as $element) {

    array_push($elements, $element->value);
}

foreach($elements as $element) {

    echo("<br />" . $element);
}

You should get a list of elements in the sioc ontology. The problem is that the first 9 are not actually part of the ontology and so I want to ignore them and start on the 10th node which has 'http://rdfs.org/sioc/ns#' as its value for the rdf:about attribute.

By including your xpath query i get nothing back at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文