3 ms·
I've done this a few times with XPath from an ipython shell, using the lxml module to parse the XML and handle the XPath queries. 30-second XPath pitch: /
by rcfox 13y ago
I've done this a few times with XPath from an ipython shell, using the lxml module to parse the XML and handle the XPath queries.
30-second XPath pitch:
//foo/bar
This gets the 'bar' children from every 'foo', regardless of where it occurs in the hierarchy.
//baz[@name='Alice']
Gets everything like: <baz name="Alice">
There's lots of more complicated things you can do, but these are the most frequent types of things I've had to do.
- barrkel 13y agoI have a command-line thing put together with HtmlAgilityPack that represents HTML in an XPath-navigable document. It lets me apply the same XPath queries to HTML, for screen scraping.
- rcfox 13y agoOh, that reminds me: You can use XPath in Chrome with $x(). To get all of the links on the current page, open up the console and type: $x('//a')
- samsquire 13y agoI've found xmlstarlet is wonderful if you are want to use XPath on the terminal. Great with curl + XML APIs like Jenkins. xml sel --template --value-of "//foo/bar" \ --template --value-of "//baz[@name='Alice']" << EOF <foo> <bar>hello</bar> <baz name="Alice"> world</baz> </foo> EOF http://xmlstar.sourceforge.net/docs.php http://xmlstar.sourceforge.net/docs.php It also can edit with XPath which is really handy.