'Compare values via XPath
On the following xml example, I need to compare data products whose minimum stock is higher than its current stock. How can i do it? I don't find any documentation.
<products>
<product>
<cod_prod>1010</cod_prod>
<name>MSI G41M-P26</name>
<price>50</price>
<current_stock>10</current_stock>
<minimum_stock>3</minimum_stock>
<cod_zone>10</cod_zone>
</product>
<product>
<cod_prod>1011</cod_prod>
<name>Micro Intel Core i5-2320</name>
<price>120</price>
<current_stock>3</current_stock>
<minimum_stock>5</minimum_stock>
<cod_zone>10</cod_zone>
</product>
<product>
<cod_prod>1012</cod_prod>
<name>Micro Intel Core i5 2500</name>
<price>170</price>
<current_stock>5</current_stock>
<minimum_stock>6</minimum_stock>
<cod_zone>20</cod_zone>
</product>
I try with this, but not works:
/products/product[minimum_stock>/products/product/current_stock]
Solution 1:[1]
The correct XPath is simpler than what you have tried :
/products/product[minimum_stock > current_stock]
Since context element inside the predicate ([]) is the current product element, you can simply say current_stock instead of /products/product/current_stock to reference current_stock element of current product, just like you already did for minimum_stock.
Solution 2:[2]
//product[number(minimum_stock)>number(current_stock)]
If you don't use the number() function, only strings are compared (the first character of both strings)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Suraj Rao |
