Just came through XPath.
I got curious about this. Why is there a different way to select tags in XPath and CSS(selectors).
Why cant there be a common way in both XPath and CSS?
Just came through XPath.
I got curious about this. Why is there a different way to select tags in XPath and CSS(selectors).
Why cant there be a common way in both XPath and CSS?
CSS is not designed to be used as a selector. It's purpose is to mark similar items in markup, so that they can be styled in the same way across the document. Because jQuery (et al) is also related to styling, and functionality is often attached to similar items in markup, CSS selectors have become commonplace in that arena.
If this doesn't explain it then imagine what an XPath Stylesheet sould look like. Perhaps something like this:
/html/body/div/table/tr/td[3]/div {
background-color: #800;
}
Now, if you add a column to the table, or add a div into the structure, or maybe redefine the first div using HTML5's <article> tag, you're going to have to go and redefine your style's path.
Or, in reverse, imagine having to put a CSS tag on everything, just so that you can find it. What a PITA that would be.
Historically, both standards grew independently, and the design goals were quite different - XPath is for selecting node sets from an XML document, while CSS is for defining layout properties of documents in various markup languages, including HTML, XHTML and SGML. Obviously, there is quite some functional overlap, but not enough to make them arbitrarily exchangeable, and the syntax is partially incompatible.
If an effort were made to unify them both, implementors would have to support both XPath and CSS selectors, and each one is complicated enough on its own (just to give you an idea, see how much of CSS3 current browsers implement, and how long that took them).
My assumption: