Show HN: Xq – command-line XML and HTML beautifier and content extractor
github.com
github.com
xq looks interesting for pure XML, will add it to my notes.
Regarding xmlstarlet, I don't use it much but here's some stuff from my notes:
XML filter on xpath: xmlstarlet sel -t -c configuration/appender logback.xml; echo
XML editing: xmlstarlet ed -d configuration/root -d configuration/logger -s configuration -t elem -n woof -v 'Woof!' logback.xml
The last one deletes elements given by xpath "configuration/root" and "configuration/logger", then adds a new element with the contents <woof>Woof!</woof> under the xpath "configuration".
It is also handy doing at some PoC work, and then one can feed it "-C" to output the backing xslt for use elsewhere
$ echo '<a><b>hello</b></a>' | xmlstarlet sel -t -m /a -c 'count(*)' --nl
1
$ echo '<a><b>hello</b></a>' | xmlstarlet sel -C -t -m /a -c 'count(*)' --nl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<xsl:for-each select="/a">
<xsl:copy-of select="count(*)"/>
<xsl:value-of select="' '"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>Have you considered adding css sectors as an alternative to xpath? For many simple things a css selector is easer to write and more people already know them.
I believe it's possible to translate css selectors to xpath so it wouldn't need another selection engine.
htmlq does this by having various command line options like --attribute and --text.
I wonder if this space of tools is a bit like static site generators: it's almost as easy to write your own as it is to learn somebody else's.
curl https://news.ycombinator.com/ | xq
yields: <title>Hacker News</title>XML syntax error on line 4: element <link> closed by </head>
1— The tool parses XML by default, which is much stricter than HTML. Hence the error you are seeing (in HTML5, one can omit various close tags and the HTML is still 100% valid — that's what you are encountering here)
HTH
It'll be nice to try something new. swiss army style command line XML tools has been pretty neglected.
https://ap-playerservices.streamtheworld.com/api/livestream?station=NOVA_919&version=1.9
so xqilla and learning xlst et al still seems like my to-go for complex documentsYou should try an XPath 3 based implementation. XQuery 3.1 has some nice features not found in XPath 2 implementations e.g. native JSON support, the arrow operator (piping), group by clause in FWLORs. XSLT 3.0 has some nice new stuff too, but I use that less
Thankfully XQilla has its own json support which was all I needed so far
$ curl -fsSL 'https://ap-playerservices.streamtheworld.com/api/livestream?station=NOVA_919&version=1.9' | xq -x //transport
http
hls
hlsts
http
http
hls
hlsts
http
hls
hlsts for $mp in /ls:live_stream_config/ls:mountpoints/ls:mountpoint
for $server in $mp/ls:servers/ls:server
return { concat(
"https://", $server/ls:ip, "/", $mp/ls:mount, ".",
local:codec-to-ext($mp/ls:media-format/ls:audio/@codec)
) }
is the core of my xqilla work, its something I could replicate in jq, I don't think I could replicate it in xq howeverWhich extracts XML and HTML as JSON.
Also cool: https://github.com/dbohdan/structured-text-tools/ "A list of command line tools for manipulating structured text data"
JC – JSONifies the output of many CLI tools
$ echo '<a xmlns="http://www.w3.org/2005/Atom"><b xmlns="urn:beta">hello</b></a>' | xq --xpath '/a/b/text()'
hello
Only partial support for functions, too, it would appear $ echo '<a xmlns="http://www.w3.org/2005/Atom"><b xmlns="urn:beta">hello</b></a>' | xq --xpath 'count(/a/*)'
$
Some A++ error handling, too, as it just calls panic when angry $ echo '<a xmlns="urn:alpha">hello</a>' | xq --xpath '/Q{urn:alpha}a/text()'
panic: /Q{urn:alpha}a/text() has an invalid token.
goroutine 18 [running]:
github.com/antchfx/xmlquery.Find(...)
github.com/antchfx/xmlquery@v1.3.8/query.go:76
...
It seems to be a problem with that backing xmlquery library, and I did a quick GitHub topics search and found https://github.com/zzossig/rabbit which claimed to be XPath 3, but then https://github.com/zzossig/rabbit#what-is-not-supported says "lol, namespaces, wat?" so :-(xsltproc