W3C home > Mailing lists > Public > public-webapps@w3.org > October to December 2011

Re: XPath and find/findAll methods

From: Martin Kadlec <bs-harou@myopera.com>
Date: Tue, 22 Nov 2011 13:31:19 +0100
Message-Id: <1321965079.29917.140661002167409@webmail.messagingengine.com>
To: "Robin Berjon" <robin@berjon.com>
Cc: public-webapps@w3.org
On Tuesday, 22 November 2011 11:50 AM, "Robin Berjon" <robin@berjon.com> wrote:
> So let's start again and do it right this time. Martin: what are your use
> cases for XPath that aren't addressed with the current support for CSS
> Selectors?
> -- 
> Robin Berjon - http://berjon.com/ - @robinberjon

Thank you for support.
In my opinion, there are two use cases for XPath:
1) It's much harder to write the CSS selector
CSS: "article > div:nth-child(2) input[type=text], article > div:nth-child(2) input:not([type]), article > div:nth-child(2) input[type=color]";
XPath: "//article/div[2]//input[@type='text' | @type='color'| not(@type)]";

2) It's impossible to write the CSS selector (or really really hard/long)
a - "*[@data-price>30]";
b - "*[position()<30]";
c - "div[@*]";
d - "//div[parent::*//a]";
e - ...

Anyway, mostly when I use XPath instead of CSS selectors it's because of XPath axis - the ability to go not just deeper but also higher in the element tree. XPath can also work with any node, not just elements.

There are definitely lots of cases when CSS selectors are much more powerful than XPath. Mainly when there are many of class names and ids involved. And that is why I think that XPath and CSS selectors are not rivals - they are exactly the opposite, they complement each other.

Received on Tuesday, 22 November 2011 12:31:47 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:26:36 UTC