Reputation: 131
The code below would work fine if the "name" element in the XML file below didn't have a leading white space. In the XPath expression ss their a way to trim leading XML element white space? Or any other way to deal with such a white space issue? Thanks!
$xmldoc = simplexml_load_file("products.xml");
$query = $xmldoc->xpath('/products/product[starts-with(name, "Desk")]');
foreach($query as $Products) {
echo $Products->name . " ";
echo $Products->price . "<br>";
}
<products>
<product type="Electronics">
<name> Desktop</name>
<price>499.99</price>
<store>Best Buy</store>
</product>
Upvotes: 3
Views: 5793
Reputation: 9644
The normalize-space
function:
/products/product[starts-with(normalize-space(name), "Desk")]
will trim trailing whitespaces from whatever you apply it on.
Upvotes: 8