XPath - crop leading space

The code below will work fine if the "name" element in the XML file below does not have a leading space. In an XPath ss expression, is there a way to crop the leading space of an XML element? Or is there any other way to deal with such a problem with a space? Thank you

$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>
+3
source share
1 answer

Function normalize-space:

/products/product[starts-with(normalize-space(name), "Desk")]

will trim trailing spaces from what you apply.

+8
source

All Articles