php xpath - string functions on a attribute
Question
What is the correct syntax for applying string functions (e.g. "normalize-space") on a attribute using the xpath query below?
Query: "./@media"
I've seen examples of this elsewhere but if I try it using php's xpath library it returns nothing...
Solution
Using query() on a DOMXPath object will always result in a node-set (wrapped in a DOMNodeList object), never in a string.
You can't pull out the results of XPath functions. You must query the nodes you want to process, and process them in PHP.
OTHER TIPS
Use evaluate() instead of query()
. It will return a typed result. It's available since PHP 5.1.
$doc = new DOMDocument();
$xpath = new DOMXpath($doc);
$q = $xpath->evaluate('"test"');
var_dump($q);
output:
string(4) "test"
./[normalize-space(@media)
Please try this.
How about this?
normalize-space(./@media)
You could also try:
normalize-space(string(./@media))
but I don't think that's neccessary here.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow