Home > Blockchain >  Xpath-How to extract a particular word from the text()?
Xpath-How to extract a particular word from the text()?

Time:04-16

Can anyone help me how to extract particular word from the text() from the Xpath expression

I'm currently scrapping the names of the coins from Website:https://coinmarketcap.com/currencies/bitcoin/

have used the Xpath expression: (//h1[@class='priceHeading']/text())[1]

which has 'Bitcoin Price' I just need the first word 'Bitcoin' ignoring the rest.

Don't mind my mistakes, I'm a newbie here :)

CodePudding user response:

Well, it kind of depends upon what you can rely on, and which version of XPath you are using. Whether a space is sufficient, or if you would want some more sophisticated tokenization is largely dependent on the data and your requirements.

With XPath 1.0 and later, you can use substring-before() a space

substring-before((//h1[@class='priceHeading']/text())[1], ' ')

With XPath 2.0 and later, you can use tokenize() and select the first item

tokenize((//h1[@class='priceHeading']/text())[1], ' ')[1]

If you know that it will always end with " Price" then you could use that value instead of just a space in the substring-before() or tokenize(), or could replace() " Price" with "''":

replace((//h1[@class='priceHeading']/text())[1], ' Price', '')
  • Related