How to Get Only Number on H1 Using XPath?

Viewed 38

How to get number on H1 Using XPath? Grab only number ( after chapter )

<header>
<h1>Example Chapter 01</h1>
<span class="epx">Kamis, 08 Sep 2022, in <i class="fas fa-book-open"></i> <a href="/manga/isekai-de-onnanoko-no-onaneta/" title="Example"> Example</a></span>
</header>
<div class="desch">Example.</div>
</div> 
1 Answers

If your XPath processor supports XPath 2.0, you can use tokenize() to parse the h1 text by space and then take the last token. This assumes the number always appears last in the h1 text preceded by a space.

//header/h1/tokenize(., ' ')[last()]

returns

01

With XPath 1.0 you may need to resort to something like using translate() to remove all alphabetic and space characters, but this may not always work depending on the exact content of your chapter titles, e.g.

//header/h1/translate(., 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ', '')

which also returns

01
Related