XPATH - Select all child nodes with a specific attribute

风格不统一 提交于 2019-12-19 08:05:37

问题


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.

Hope this makes sense.


回答1:


$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



回答2:


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.



来源:https://stackoverflow.com/questions/2488021/xpath-select-all-child-nodes-with-a-specific-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!