mrb
mrb

Reputation: 193

How to select all attributes under an element using xmlstarlet?

How to use xmlstarlet to get the hrefs from the following div?

<div xmlns="http://www.w3.org/1999/xhtml" class="my_list">
<ul>
<li><a href="http://mylink1.html" title="title1">this is title 1</a></li>
<li><a href="http://mylink2.html" title="title2">this is title 2</a></li>
 ...
 </ul>
</div>

I tried the following xmlstarlet sel -t -m "//*[@class='my_list']" -v "." myfile.xmlon my mac, but without success.

Upvotes: 2

Views: 1322

Answers (1)

kjhughes
kjhughes

Reputation: 111561

You can append //@href to your XPath expression to get all @href attributes beneath the nodes it already selects:

//*[@class='my_list']//@href

Explanation:

Your base XPath was fine for selecting the div of interest. It bypasses the need to declare a namespace for http://www.w3.org/1999/xhtml by using the element wildcard, * and then specifying a value for @class, which is not affected by the default namespace declaration on the div of interest.

From there, // selects along the descendent-or-self axis -- / would only select along the immediate child axis -- and @href selects the href attributes requested.

Upvotes: 1

Related Questions