4

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?

2
  • Shouldn't this be on Stack Overflow? Anyway, the first thing you need to bear in mind is that CSS was originally designed for HTML, and XSLT and XPath are designed for XML. Two different style languages for two different markup languages. CSS can be used to style XML, and XSLT transforms XML into (X)HTML, but that's probably beyond the scope of all this...
    – BoltClock
    Oct 18, 2011 at 11:55
  • @BoltClock - where's the code? Seriously though - this wouldn't be on topic for Stack Overflow.
    – ChrisF
    Oct 18, 2011 at 12:02

3 Answers 3

4

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.

1
  • +1 for the 'anti-example'
    – n611x007
    Apr 19, 2013 at 9:47
1

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).

0

My assumption:

  • XPath is much more powerful when it comes to selecting nodes, allowing some very complex conditions
  • while CSS selectors aren't that powerful, they are powerful enough
  • XPath is newer than CSS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.