2

I would like to extract number/float value from this code using Xidel:

<p class="price">
    <span class="woocommerce-Price-amount amount">
        <bdi>
            304.00
            <span class="woocommerce-Price-currencySymbol">
                €
            </span>
        </bdi>
    </span>
</p>

I am trying the following command:

xidel -s '<p class="price"><span class="woocommerce-Price-amount amount"><bdi>304.00 <span class="woocommerce-Price-currencySymbol">€</span></bdi></span></p>' -e "//p[@class='price']/translate(normalize-space(substring-before(., '€')),' ','')"

The translate command should replace space, but it's not working, in the output I still see one space after number "304.00_".

Adrian
  • 2,576
  • 9
  • 49
  • 97

2 Answers2

1

Try changing the xpath expression to

-e  "substring-before(//p[@class='price']//bdi/normalize-space(.),' ')"

or

 -e "substring-before(//p[@class='price']//bdi/.,' ')"

or use tokenize()

 -e "tokenize(//p[@class='price']//bdi/.,' ')[1]"

The output should be

'304.00'
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • It works for the example above, but for some reason on the actual [website](https://kenzel.sk/produkt/bicykle/zivotny-styl/signora/) (with same code) not working. Weird. – Adrian Dec 13 '21 at 00:21
  • @Adrian Weird indeed. Whatever that is on the actual website, it's not any of the regular space characters. Maybe someone smarter can figure that out. In the meanwhile, there is a way to retrieve that data, but it's very hacky. I can edit the answer, if you're interested. – Jack Fleeting Dec 13 '21 at 01:21
1

You're going to have to process the no-break space separately with one of the following queries:

-e "//p[@class='price']/span/bdi/substring-before(text(),'&#160;')"
-e "//p[@class='price']/span/bdi/translate(text(),x:cps(160),'')"
-e "//p[@class='price']/span/bdi/replace(text(),'&#xA0;','')"

You can't use normalize-space(), because...

https://www.w3.org/TR/xpath-functions-31/#func-normalize-space:

The definition of whitespace is unchanged in [Extensible Markup Language (XML) 1.1 Recommendation]. It is repeated here for convenience:

S ::= (#x20 | #x9 | #xD | #xA)+

...it processes spaces, tabs, carriage returns and line feeds, but not no-break spaces:

xidel -s "<x>   test   </x>" -e "x'[{x}]'"
[   test   ]

xidel -s "<x>   test   </x>" -e "x'[{normalize-space(x)}]'"
[test]

xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{x}]'"
[   test   ]

xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{normalize-space(x)}]'"
[   test   ]

xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{translate(x,'&#160;','')}]'"
xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{replace(x,x:cps(160),'')}]'"
xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{replace(x,'&#xA0;','')}]'"
[test]

Btw, an alternative to get the price on that website:

xidel -s "https://kenzel.sk/produkt/bicykle/zivotny-styl/signora/" -e ^"^
  parse-json(^
    //body/script[@type='application/ld+json']^
  )//priceSpecification/price^
"
304.00
Reino
  • 3,203
  • 1
  • 13
  • 21