Um XML parsing is literally natively supported. And no, Cheerio doesn’t let you do them. Cheerio just allows you to do query selection from node since you can’t access the DOM without a browser. But it still has all the limits of queryselector. Now you can use additional JavaScript to do the above things, but why write multiple lines of code to fetch a set of nodes when you could write one xpath?
And as a reminder, this is all still in the confines of JS. Xpath can be used with almost any language and framework.
That's front-end only. So yes, if you're a masochist you can load html in a headless browser and evaluate xpath expressions there, but Cheerio does just fine. The people I see still using xpath for things like this are generally Python coders.
There are numerous node modules for xpath, just as easy to install and use as cheerio. And I’m not sure what people you’re talking about, but I’ve worked in tech for over a decade including two RPA companies and every major player in the space relies on xpath.
If you truly believe cheerio and queryselector give you superior form and function, then I’d challenge you this: using those tools, write a selector of equal or lesser size that will perform the same as the example below from my previous comment.
Descendent-based ancestor selection - Let's say you want to get the parent div of every a with the class "child". For xpath, that's simply "//a[@class='child']/parent::div". With queryselector you can only travel down the ancestry axis, not up.
That solution has a worse performance ratio and hard-codes half the path. As for your remark about going back up the dom, you’ve clearly never done RPA in a b2b setting. When you don’t have control over the original DOM and have to accommodate instabilities, it’s often much easier to navigate up from a target element.
Again, there is no JS equivalent of lxml so this is just how we do it. You're wrong about in-browser performance though, xpath is always slower than css. You're also wrong about my iterating comment, you can just as easily iterate the parent element first, code like yours is just lazy.
4
u/[deleted] Jun 02 '20
Um XML parsing is literally natively supported. And no, Cheerio doesn’t let you do them. Cheerio just allows you to do query selection from node since you can’t access the DOM without a browser. But it still has all the limits of queryselector. Now you can use additional JavaScript to do the above things, but why write multiple lines of code to fetch a set of nodes when you could write one xpath?
And as a reminder, this is all still in the confines of JS. Xpath can be used with almost any language and framework.