Web Design Forum: CSS to XPATH in PHP - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CSS to XPATH in PHP Rate Topic: -----

#1 User is offline   php_penguin 

  • richthegeek
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,471
  • Joined: 06-August 07
  • Reputation: 7
  • Gender:Male
  • Location:Liverpool
  • Experience:Web Guru
  • Area of Expertise:Coder

Posted 03 November 2007 - 01:00 AM

As part of writing a little CSS based PHP templating engine, I did a bit of searching for a CSS to XPATH translator, as many more people know CSS than XPath.

Anyhow, the only ones I could find were in JavaScript, so I had to translate from JS to PHP... which was fun.. well less boring and certainly less painful than you might tihnk.

Anyway, I based it on Joe Hewitt (of firebug)'s code and once I had ironed out the kinks, it worked brilliantly!!

Here is my code (nice and long, so it is attached) for creating an XPATH query from a CSS selector.

I've tested it out on "div.bold.italic#content" and it works great, as does with direct- and any-child selectors, as well as multiple selections at once.

Any comments? Surprised at how close the code is between JS and PHP?

Attached File(s)


0

#2 User is offline   php_penguin 

  • richthegeek
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,471
  • Joined: 06-August 07
  • Reputation: 7
  • Gender:Male
  • Location:Liverpool
  • Experience:Web Guru
  • Area of Expertise:Coder

Posted 03 November 2007 - 02:57 AM

ok, here is an updated version.

It now has support for the following selectors:

:first
:first(+n)
:last
:last(-n)
:nth-child(n)
:gt(n)
:lt(n)

lots of lovely ****** regex

Attached File(s)


0

#3 User is offline   Thomas Thomassen 

  • HTTP 503
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,254
  • Joined: 30-April 07
  • Reputation: 10
  • Gender:Male
  • Location:Trondheim, Norway
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 03 November 2007 - 10:33 AM

This is a great little snippet.
0

#4 User is offline   xwisdom 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 9
  • Joined: 30-March 08
  • Reputation: 0
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 19 March 2009 - 12:35 AM

Hello,

Many thanks for this snippet. It's really great!

I've modified it a bit and have included into into the Raxan Framework. You can learn about it here http://raxanpdi.com

__
Raymond Irving
0

#5 User is offline   Didi 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 18-January 12
  • Reputation: 0

Posted 18 January 2012 - 06:05 PM

View Postphp_penguin, on 03 November 2007 - 01:00 AM, said:

As part of writing a little CSS based PHP templating engine...


Thanks for the script !

There is an issue with css selectors starting with a simple class:
On the input ".run", the output is //[contains(@class, 'run')] with is not a valid xpath
I think that the correct xpath should be //*[contains(@class, 'run')] and
I used the following function "CSStoValidXpath" over "CSStoXpath" to patch thoses cases

function CSStoValidXpath($rule)
{
	return str_replace('//[','//*[',CSStoXpath($rule));
}

This post has been edited by Renaissance-Design: 18 January 2012 - 06:30 PM
Reason for edit: Please use the code button or tags to format your code.

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users