I am using XPATH (with PHP) to retrieve XML data based on the XML attributes. The XML structure is like this:
<products>
<product category="Desktop">
<name> Dell Desktop (d)</name>
<price>499.99</price>
<shipping cost="10.99" carrier="UPS" />
</product>
</products>
This code works fine, as it tests the XML element <product> which only has ONE attribute - named "category".
$productVar = "Desktop";
$x_path = $XMLproducts->xpath("/products/product[@category='$productVar']");
foreach($x_path as $Product) {
echo $Product->name . " - " . $Product->price . "<br>";
}
But the next two foreach loops are NOT displaying anything. They test the XML element <shipping> which has TWO attributes - "cost" and "carrier".
$shippingCarrier = "UPS";
$xpath = $XMLproducts->xpath("/products/shipping[@carrier='$shippingCarrier']");
foreach($xpath as $Product) {
echo $Product->name . " - " . $Product->price . "<br>";
}
$shippingCost = "UPS";
$xpath = $XMLproducts->xpath("/products/shipping[@cost='$shippingCost']");
foreach($xpath as $Product) {
echo $Product->name . " - " . $Product->price . "<br>";
}
Any advice? Thanks!