Reputation: 729
I'm trying to catch content of <div class="num">
without child element from the below HTML code:
<div class="num">
<del>14,490</del>
13,490
</div>
So I'm expecting 13,490
as result.
I use the following xpath expression for that:
//div[@class="num"]/node()[not(self::del)]
which works fine for this example, but the problem is that the content of the div may vary and sometimes it doesn't contain the child element.
I'm using Python and can handle this on coding level but I'm curious whether this can be done by xpath only.
Upvotes: 2
Views: 687
Reputation: 474161
You can just get the text()
:
//div[@class="num"]/text()
Demo (using xmllint
):
$ xmllint index.html --xpath '//div[@class="num"]/node()[not(self::del)]'
13,490
$ xmllint index.html --xpath '//div[@class="num"]/text()'
13,490
This would also work if there is no del
tag:
<div class="num">
13,490
</div>
Upvotes: 3