antchfx/htmlquery

Using Libraries to Convert HTML to XPath

ProfessorManhattan opened this issue · 5 comments

Hey, this is not an issue but I have been unable to understand why there are not many reputable packages that advertise the ability to turn CSS/HTML to XPaths. Can I use this library to convert CSS to an XPath?

No, this is an XPath package for XML/HTML/JSONdocuments query using XML query syntax. CSS Selector is another query syntax, there are cannot work together.

@zhengchun -- shouldn't it be possible to convert a CSS selector to an XPath as long as the CSS selector only returns one element?

You can do this in manually If you known CSS selector syntax and XPath syntax.

For example:

  • CSS selector: a>img, is equivalent to XPath: //a/img
  • CSS selector: a[href], is equivalent to XPath: //a[@href]
  • CSS Selectoe: .class_name, is equivalent to XPath: [class='class_name']

Hey @zhengchun --- thanks but if I do anything manually then my computer becomes the boss -- trying to ensure the opposite. There are a few libraries, the most popular of which is in PHP:

https://github.com/symfony/css-selector

And here's one in Node.js (with a lot less stars):

https://github.com/css2xpath/css2xpath