Reputation: 41
I have a file with an structure similar to this one:
<log id="1" date="11/11/1999">
<data>
<param id="1" value="30">
<param id="2" value="John">
<param id="3" value="Success">
</data>
</log>
<log id="2" date="11/12/1999">
<data>
<param id="1" value="20">
<param id="2" value="Albert">
<param id="3" value="Failure">
</data>
</log>
I've been trying to use grep
or xmllint
to show only the log tag where an specific value is provided (i.e. 30). My idea is that, by searching the number 30 or John it retrieves the entire log entry where those values exist. So if I searched for 30 it would return:
<log id="1" date="11/11/1999">
<data>
<param id="1" value="30">
<param id="2" value="John">
<param id="3" value="Success">
</data>
</log>
What's the best way to do this?
Thanks in advance.
Upvotes: 2
Views: 607
Reputation: 184955
Using an HTML parser with your broken input file (with xpath):
<root>
<log id="1" date="11/11/1999">
<data>
<param id="1" value="30">
<param id="2" value="John">
<param id="3" value="Success">
</data>
</log>
<log id="2" date="11/12/1999">
<data>
<param id="1" value="20">
<param id="2" value="Albert">
<param id="3" value="Failure">
</data>
</log>
</root>
$ saxon-lint --html --xpath '//param[@value="30"]/ancestor::log' file.html
<log xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml" id="1" date="11/11/1999">
<data>
<param valuetype="data" id="1" value="30"/>
<param valuetype="data" id="2" value="John"/>
<param valuetype="data" id="3" value="Success"/>
</data>
</log>
Check https://github.com/sputnick-dev/saxon-lint (my own project)
Upvotes: 0
Reputation: 69189
You can use xpath
to query for specific nodes
xpath -e '//param[@value="30"]/../..'
this returns the parent's parent of a node with a param with value 30
Upvotes: 3