Difference between revisions of "Xml grep"

From Apertium
Jump to navigation Jump to search
Line 1: Line 1:
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.
+
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, without falling for the tempation to [[How_can_I_parse_XML_with_regular_expressions|parse XML with regex]].
   
 
Specifying the full path and the full pardef name:
 
Specifying the full path and the full pardef name:
Line 58: Line 58:
 
</pre>
 
</pre>
   
  +
==External links==
  +
* http://oreilly.com/perl/excerpts/system-admin-with-perl/ten-minute-xpath-utorial.html
   
 
[[Category:Documentation in English]]
 
[[Category:Documentation in English]]

Revision as of 07:38, 16 May 2013

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, without falling for the tempation to parse XML with regex.

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


To get all c attributes:

$ xmllint --xpath '//@c' apertium-eo-en.en.dix

To get c attributes only from <e> elements:

$ xmllint --xpath '//e/@c' apertium-eo-en.en.dix

To get all attributes of the e element that has the lm "cake":

$ xmllint --xpath '//e[lm="cake"]/@*' apertium-eo-en.en.dix


To get the second dictionary section:

$ xmllint --xpath '/dictionary/section[2]/' apertium-eo-en.en.dix

(or section[position()=2])


Some corpora are formatted in XML and put e.g. the real text contents inside a particular element. Say the corpus puts all text inside <sentence> elements, you can grep them out with:

$ xmllint --xpath '*/sentence/text()' corpus.xml

External links