13 ms·
I find CSS selectors a lot easier to write than XPath. I recently gave a talk on how PHP's new DOM API makes working with HTML and CSS selectors natively very e
by k1m 6mo ago
I find CSS selectors a lot easier to write than XPath. I recently gave a talk on how PHP's new DOM API makes working with HTML and CSS selectors natively very easy (previously you had to convert CSS to XPath).[1]
It's a shame that because CSS is still primarily for browser use and styling, we don't get nice things like the ability to select based on text content like we can with XPath. My understanding is that this was proposed but didn't make it into the spec because it could lead to performance issues in a browser rendering context.
[1] https://speakerdeck.com/keyvan/parsing-html-with-php-8-dot-4?slide=40 https://speakerdeck.com/keyvan/parsing-html-with-php-8-dot-4...
- deleted 6mo ago[deleted]
- zerocrates 6mo agoYeah, querySelector/querySelectorAll are totally widespread in client-side, it's nice to finally have them in PHP's newer DOM. Definitely what people are used to doing.
- werdnapk 6mo agodocument.evaluate is also widespread client-side. https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate https://developer.mozilla.org/en-US/docs/Web/API/Document/ev...
- zerocrates 6mo agoSure, I was more speaking to usage... I'd have to imagine that querySelector* dwarfs evaluate in actual usage. Chrome's stats put evaluate at about 5% of loads; I'm not sure they have tracking for querySelector. Of course XPath has its place and is the better tool for some situations. On the PHP side XPath has been supported for a long time, while the querySelector stuff is quite new.
- lewisjoe 6mo agoWhat's more fun is: LLMs too are really good at CSS selectors. I've been building a document editing agent and I used this fact to present the document as HTML to LLM and make it query and pull pieces of documents into context by just specifying CSS selectors. Works like magic!