From what I can gather, XPATH pin-points exact element nodes, whereas CSS Selectors will only do so if you provide the ID of a element, but anything given after applies to all child elements???
Using XPATH is it possible to specify something like:
I need a way to use an easy syntax like XPATH or CSS selectors to pin point exact elements much like one would traversing a DOM using getElementByID or similar...
It depends. When I use XPath in my XSLT stylesheets, everything's recursive, so there's no need to use the descendant axis. When I use XPath to retrieve nodes from the DOM... well... it still depends on the circumstances, but if I'm using an absolute path, probably there needs to be an ID on that element.
// select all acronym nodes in a document without title attributes
$nodes = $this->query("//html:acronym[not(@title)]");
// select the first head element in a document
$head = $this->query("//html:head")->item(0);
// select a div with the toc id attribute
$container = $this->query("//html:div[@id='toc']")->item(0);
// select all paragraphs that do not have a preceding paragraph sibling
$nodes = $this->query("//html:p[local-name(preceding-sibling::*[1])!='p']");
Looking at these, it seems that I tend to use the descendant axis. But then again, these are very general XPath queries.
Relative paths start from the current context node.
E.g. in php's DOM implementation you can pass that context node as second parameter to the method XPath::query()
Simple as all it does it point? to the element with an ID of 'foot'
Here is the problem. I need to point inside the first <div> inside the <div id='foot'> but without using any ->item(0) techniques, strictly using XPATH is this possible? Something like:
Php's DOMXPath::query allways returns a DOMNodeList, even if there is exactly one Node in it.
Other dom implementations may provide additional methods, like e.g. XmlNode.SelectSingleNode but there's no standard for that.