Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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 '11 at 11:55
@BoltClock - where's the code? Seriously though - this wouldn't be on topic for Stack Overflow. – ChrisF Oct 18 '11 at 12:02

3 Answers

up vote 4 down vote accepted

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.

share|improve this answer
+1 for the 'anti-example' – naxa Apr 19 at 9:47

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
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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