Using xidel to extract a key-value pair

1.4k views Asked by At

I have multiple tables on a website like so:

<table>
  <tr>
    <td>Name</td>
    <td>foo</td>
  </tr>
  <tr>
    <td>Count</td>
    <td>15</td>
  </tr>
  <tr>
    <td>Date</td>
    <td>2014-11-17</td>
  </tr>
</table>
<table>
  <tr>
    <td>Name</td>
    <td>bar</td>
  </tr>
  <tr>
    <td>Count</td>
    <td>42</td>
  </tr>
  <tr>
    <td>Date</td>
    <td>2014-12-24</td>
  </tr>
</table>
...

I want to receive something like this

foo    15
bar    42

My first attempt in xidel was xidel --xpath "//table/tr[1]/td[2]" --xpath "//table/tr[2]/td[2]" but this is giving

foo
bar

15
42

How can I extract two values in one line?

2

There are 2 answers

0
Martin Honnen On

Using XPath or XQuery 3.0: //table/tbody/(tr[1]/td[2] || ' ' || tr[2]/td[2]). I think you need to request that version explicitly, at least I needed to do so on http://videlibri.sourceforge.net/cgi-bin/xidelcgi. And I parsed as HTML where the parser adds a tbody element and the path needs that too.

1
Reino On
xidel-0.9.5.4998.exe -s --input-format=xml <input> ^
                     --xquery "//table/concat(tr[1]/td[2],'&#09;',tr[2]/td[2])"
foo     15
bar     42