Since xq bundled along with kislyuk/yq uses jq underneath for DSL, you can use the tonumber function to convert the .count to their numeric equivalent
Copyxq '.groups.a | map( (.count, .max) |= tonumber )' xml
[
{
"count": 1,
"max": 46,
"name": "foo"
},
{
"count": 3,
"max": 81,
"name": "bar"
}
]
Answer from Inian on Stack OverflowHow to parse XML into JSON with xq and output specific elements as numbers? (instead of strings) - Stack Overflow
Show HN: Xq – command-line XML and HTML beautifier and content extractor
xq: command-line XML beautifier and content extractor (written in Go)
yq - Parsing XML to CSV using xq and jq - Stack Overflow
Since xq bundled along with kislyuk/yq uses jq underneath for DSL, you can use the tonumber function to convert the .count to their numeric equivalent
Copyxq '.groups.a | map( (.count, .max) |= tonumber )' xml
[
{
"count": 1,
"max": 46,
"name": "foo"
},
{
"count": 3,
"max": 81,
"name": "bar"
}
]
A general approach to your task is to go through all the nodes recursively and try to convert them into numbers.
xq 'walk(if type == "string" then tonumber? // . else . end)'
If you just want to update a specific part of your input you can do this:
xq '.groups.a |= walk(if type == "string" then tonumber? // . else . end)'
See also converting strings to numbers in jq
For BaseX, to evaluate file.xq against the contents in in.xml and write the result to out.xml:
basex -iin.xml -oout.xml file.xq
Much more documentation is available on the wiki.
For Saxon, to perform the same operation:
java net.sf.saxon.Query -s:in.xml -q:file.xq -o:out.xml
Documentation likewise available.
If you like packaged bundles, you might be interested by the EXPath repository manager. It comes with Saxon and Calabash (you can pick which to install), as well as scripts to run them with proper classpath, class, etc. It comes with a graphical installer.
To install it:
- go to http://expath.org/files/pkg
- download the latest
expath-repo-installer-*.jar - execute it (e.g.
java -jar ~/Downloads/expath-repo-installer-*.jar) - (optional) add the dir
bin/where it has been installed to your PATH
On the shell, just invoke saxon --help (or ~/expath/pkg/bin/saxon --help or any ther place you installed it if you did not put it in the PATH), and follow the instructions. All options like --xxx at the beginning of the command line are consumed by the script itself. All the rest is passed as is to Saxon.
For instance to execute a query from the command line, with a specific document as the context item:
saxon --xq -s:data.xml -q:query.xq