Thufir
Thufir

Reputation: 8497

what is the xpath syntax to grab html tag elements?

how do I print the title value for the below html file using xmlstarlet?

thufir@doge:~/.html$ 
thufir@doge:~/.html$ xmlstarlet sel -t -v "/html/header[@name='title']" -n hello.html 

thufir@doge:~/.html$ 
thufir@doge:~/.html$ cat hello.html 
<html>
<header><title>This is title</title></header>
<body>
Hello world
</body>
</html>
thufir@doge:~/.html$ 

Grabbing xml might be a bit different than html? Assuming garden-variety html and not xhtml.

The reason I'm using xmlstarlet is specifically to use xpath syntax which seems rather alien.

Upvotes: 0

Views: 2206

Answers (2)

Ga&#235;l Barbin
Ga&#235;l Barbin

Reputation: 3919

With:

"/html/header[@name='title']"

you select an header element which has an attribute name with the value "title".

What you want is to grab a title element in an header element:

//header/title

or just use :

//title

which selects all title elements, regardless of its position in the tree.

Upvotes: 2

jdphenix
jdphenix

Reputation: 15435

I'd just cheat and use Chrome's Developer Tools.

Open the HTML in Chrome, open the Developer Tools, then in the Elements tab, right click and select Copy > Copy XPath.

/html/body/header/title

Upvotes: 1

Related Questions