简体   繁体   中英

XPath - trim the leading white space

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>

The normalize-space function:

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

will trim trailing whitespaces from whatever you apply it on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM