tags:

views:

1023

answers:

4

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...

A: 
./[normalize-space(@media)

Please try this.

Mork0075
Yep I've tried that and it returns this error - Catchable fatal error: Object of class DOMElement could not be converted to string
EddyR
A: 

How about this?

normalize-space(./@media)

You could also try:

normalize-space(string(./@media))

but I don't think that's neccessary here.

joeytwiddle
I still get the same error
EddyR
+1  A: 

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.

Tomalak
great - so much for efficient coding! I'm slowly learning the pitfalls of php's old xpath library :) here's hoping they move to 2.0 sometime soon!
EddyR
I doubt the version of XPath used or the age of the PHP library has anything to to with it. Can you confirm that this behavior will be going to change when XPath 2.0 will be supported, or do you just somehow hope so?
Tomalak
+1  A: 

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"
culebrón
Nice addition, +1.
Tomalak