Reputation: 2404
I'm writing a simple bash script to parse some xml. I was using sed and awk but I think xmllint is better suited.
Unfortunately I'm completely new to xpath so I'm really battling.
I'm trying to take the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<releaseNote>
<name>APPLICATION_ercc2</name>
<change>
<date hour="11" day="10" second="21" year="2013" month="0" minute="47"/>
<submitter>Automatically Generated</submitter>
<description>ReleaseNote Created</description>
</change>
<change>
<version>2</version>
<date hour="11" day="10" second="25" year="2013" month="1" minute="47"/>
<submitter>fred.bloggs</submitter>
<description> first version</description>
<install/>
</change>
<change>
<version>3</version>
<date hour="12" day="10" second="34" year="2013" month="1" minute="2"/>
<submitter>fred.bloggs</submitter>
<description> tweaks</description>
<install/>
</change>
<change>
<version>4</version>
<date hour="15" day="10" second="52" year="2013" month="1" minute="38"/>
<submitter>fred.bloggs</submitter>
<description> fixed missing image, dummy user, etc</description>
<install/>
</change>
<change>
<version>5</version>
<date hour="17" day="10" second="31" year="2013" month="1" minute="40"/>
<submitter>fred.bloggs</submitter>
<description> fixed auth filter and added multi opco stuff</description>
<install/>
</change>
.....
and process it to pass in '3' as the variable to an xpath script, and output something like this:
4 fred.bloggs 10/1/2013 15:38 fixed missing image, dummy user, etc
5 fred.bloggs 10/1/2013 17:40 fixed auth filter and added multi opco stuff
In other words, a complex combination of the contents of each node, where the value of version is greater than, for example, 3.
Upvotes: 2
Views: 1592
Reputation: 241931
One tool you might find useful for this sort of thing is xmlstarlet, although using an xpath tool might be less idiosyncratic.
With xmlstarlet
, the following works (I added a close tag for releaseNote to your example):
$ summary() {
xmlstarlet sel -t -m "//change[version > $2]" \
-v submitter -o $'\t' \
-v date/@day -o '/' -v date/@month -o '/' -v date/@year -o ' ' \
-v date/@hour -o ':' -v date/@minute -o $'\t' \
-v description -n $1
}
$ summary test.xml 3
fred.bloggs 10/1/2013 15:38 fixed missing image, dummy user, etc
fred.bloggs 10/1/2013 17:40 fixed auth filter and added multi opco stuff
$
Upvotes: 3