XPath (kind of) as CSS Selectors

I've been noticing a convergence recently of XPath and CSS Selectors 
recently. It becomes very obvious when looking at CSS3.

After having read the arguments back and forth, I noticed one of big 
complaints was that performance would be an issue and that CSS selectors 
make sure that they can be calculated by their parents and immediate 
sibblings only.

But there is no reason that one couldn't work with the XPath spec to create 
a subset of XPath axes and functions for use in CSS. The problematic ones as 
I see it are anything that looks at descendants of an element. Therefore you 
simply remove these axes.

That still leaves you with a powerful set of selectors and more importantly 
a unified syntax. I forsee more and more elements being added to both 
languages and them growing apart in places, but for the most part ending up 
with the same feature set and slightly different languages.

Another possibility is to define CSS selectors and XPath selectors in a 
common model. This would at least allow additional languages to make sure 
that they were compatible with a single model.

Some basic example of how CSS3 selectors use the same model as XPath 1.0

CSS: *
XPath: *

CSS: E
XPath: E

CSSr: E[foo]
XPath: E[@foo]

CSS: E[foo="bar"]
XPath:  E[@foo="bar"]

CSS: E:root
XPath: /E[self:E]

CSS: E:nth-child(n)
XPath: E[position() mod n = 1]

CSS: E:nth-last-child(n)
XPath: E[(count() - position()) mod n = 1]

CSS: E:first-child
XPath: E[1]

CSS: E:last-child
XPath: E[last()]

CSS: E:empty
XPath: E[not(*)]

CSS:  E.warning
XPath: E[@class="warning"]

CSS: E#myid
XPath: E[@id="myid"]

CSS: E F
XPath: E//F

CSS: E > F
XPath: E/F


* The following CSS selectors have no example because I couldn't come up 
with one in a short period of time. Some of them may be possible. Feel free 
to insert examples that work. *
CSS: E[foo~="bar"]
CSS: E[foo^="bar"]
CSS: E[foo$="bar"]
CSS: E[foo*="bar"]
CSS: E:nth-of-type(n)
CSS: E:nth-last-of-type(n)
CSS: E:first-of-type
CSS: E:last-of-type
CSS: E:only-child
CSS: E:only-of-type
CSS: E+F
CSS: E~F

For some of these, CSS could introduce axes on top of XPath's existing axes. 
The point of this is even if they don't use the same syntax, it would be of 
great use for them to use the same model. That way languages can use the 
same data and improvements to one model get spread accross all models.

Now to go meet my 4:00 deadline.

Orion Adrian

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar – get it now! 
http://toolbar.msn.com/go/onm00200415ave/direct/01/

Received on Monday, 19 April 2004 13:07:37 UTC