Search code examples
phpxpathfilter

PHP XML Xpath get price lower than doesn't work


<Product>
  <Price>
   <SalePrice>
      14,95
   </SalePrice>
  </Price>
</Product>  
<Product>
  <Price>
   <SalePrice>
     16,95
   </SalePrice>
  </Price>
</Product>



$response->xpath('Product/Price/[SalePrice <= "16,95"]/parent::*

Hello, i wanna get every Sale Price equals or lower than 16,95 as it is in my example. But everytime i try, it gives a empty array. If i only use:

$response->xpath('Product/Price/[SalePrice = "16,95"]/parent::*

i got a result. I hope you can help me with this problem. Thanks


Solution

    1. translate comma to point to get correct float number
    2. convert string to number
    3. compare result with float number but not string

      //Product/Price/SalePrice[number(translate(.,',','.'))<= 16.95]/parent::*

    demo on eval