xmlstarlet sel table data

464 views Asked by At

I am attempting to retrieve table rows from the following xml snippet. The path leading up to the snippet is //html/body/div/div/div. I need to select the table data rows where the /table/tr/th/a@title='Music genre'

I can select the tr row as:

xmlstarlet sel -t -v "//html/body/div/div/div/table/tr/th/a[@title='Music genre']" Unwritten_Law

I can select the data rows as:

xmlstarlet sel -t -v "//html/body/div/div/div/table/tr/td/a" Unwritten_Law

What I need to do is select only the data rows where the tr row is Music genre

<table class="infobox vcard plainlist" style="border-spacing:3px;width:22em">
    <tr>
        <th colspan="2" style="text-align:center;font-size:125%;font-weight:bold;background-color:    #b0c4de"><span class="fn org">Unwritten Law</span></th>
    </tr>
    <tr>
        <th scope="row" style="text-align:left">Origin</th>
        <td><a href="/wiki/San_Diego,_California" title="San Diego, California" class="mw-redirect">San Diego, California</a>, U.S.</td>
    </tr>
    <tr>
        <th scope="row" style="text-align:left"><a href="/wiki/Music_genre" title="Music genre">Genres</a></th>
        <td><a href="/wiki/Pop_punk" title="Pop punk">Pop punk</a>, <a href="/wiki/Punk_rock" title="Punk rock">Punk rock</a>, <a href="/wiki/Skate_punk" title="Skate punk">Skate punk</a>, <a href="/wiki/Post_grunge" title="Post grunge" class="mw-redirect">Post grunge</a>, <a href="/wiki/Alternative_rock" title="Alternative rock">Alternative rock</a>, <a href="/wiki/Melodic_hardcore" title="Melodic hardcore">Melodic hardcore</a></td>
    </tr>
    <tr>
        <th scope="row" style="text-align:left">Years active</th>
        <td>1990–present</td>
    </tr>
</table>
1

There are 1 answers

0
npostavs On

Given the snippet in the question:

xmlstarlet sel -t -v "/table/tr[th/a/@title='Music genre']/td/a" Unwritten_Law

outputs

Pop punk, Punk rock, Skate punk, Post grunge, Alternative rock, Melodic hardcore

Add /html/body/div/div/div as needed.