Addressing Attributes

Examples thus far have dealt with element nodes in particular. XPath also supports the selection of attribute nodes.

// Returns the id attribute nodes of all ul nodes
$list = $xpath->query('//ul/@id');

// Returns all ul nodes that have id attribute nodes
$list = $xpath->query('//ul/@id/..');

// Returns all id attribute nodes
$list = $xpath->query('//@id');
?>
  • //ul/@li addresses all id attribute nodes associated with ul element nodes that are descendants of the context node.
  • Note how the use of relative addressing in the second example applies to attributes nodes just as it would apply to child element nodes.
  • //@id addresses all id attribute nodes that are descendants of the context node regardless of the element with which they are associated. 

© DOM Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (01.09.2014)
Views: 292 | Rating: 0.0/0
Total comments: 0
avatar