Difference between revisions of "Xml grep"

From Apertium
Jump to navigation Jump to search
(Created page with 'When working with xml, you'll often want to grep out an element that spans several lines. This can be hacked with awk or perl, but a more elegant solution is to use the parser in…')
 
m
Line 27: Line 27:
   
   
[[Documentation]]
+
[[Category:Documentation]]

Revision as of 11:53, 20 March 2012

When working with xml, you'll often want to grep out an element that spans several lines. This can be hacked with awk or perl, but a more elegant solution is to use the parser in libxml2 (which is a requirement when installing apertium, so should be installed on your system already). This lets you use a simple version of XPath expressions to grep out full XML elements.

Examples:

# Specifying the full path and the full pardef name:
$ xmllint --xpath '/dictionary/pardefs/pardef[@n="gen__apos"]' apertium-eo-en.en.dix
<pardef n="gen__apos">
  <e>       <p><l/>          <r/></p></e>
  <e>       <p><l>'</l>         <r><j/>'<s n="gen"/></r></p></e>
</pardef>

# But for dix files, it should be the same if you specify a relative path:
$ xmllint --xpath '//pardef[@n="gen__apos"]' apertium-eo-en.en.dix
<pardef n="gen__apos">
  <e>       <p><l/>          <r/></p></e>
  <e>       <p><l>'</l>         <r><j/>'<s n="gen"/></r></p></e>
</pardef>

# You can also search for substrings by using the 'contains' function:
$ xmllint --xpath '//pardef[contains(@n,"_adj")]' apertium-eo-en.en.dix
<pardef n="expensive__adj">
  <e>       <p><l/>          <r><s n="adj"/></r></p></e>
</pardef>
<pardef n="ca__adj">…
# etc; gives all the adj pardefs