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

Jump to content

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 

  • Web Guru
  • PipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 1,459
  • Joined: 06-August 07
  • 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)


Posted Image If someone helps you, recommend them for MOTM (PM Ben or Dizi) || UKYIA #11
while( !$dead ) {
		 code();
	 }


0

#2 User is offline   php_penguin 

  • Web Guru
  • PipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 1,459
  • Joined: 06-August 07
  • 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)


Posted Image If someone helps you, recommend them for MOTM (PM Ben or Dizi) || UKYIA #11
while( !$dead ) {
		 code();
	 }


0

#3 User is offline   Thomas Thomassen 

  • HTTP 503
  • View blog
  • Group: Member of the Month
  • Posts: 2,250
  • Joined: 30-April 07
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Archery, design, typography, monster killer robots from outer space
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 03 November 2007 - 10:33 AM

This is a great little snippet.
/(bb|[^b]{2})/ —Shakespeare
0

#4 User is offline   xwisdom 

  • Forum Newcomer
  • Pip
  • View blog
  • Group: Members
  • Posts: 9
  • Joined: 30-March 08
  • 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

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