Lee
Lee

Reputation: 4323

Why is this XPath expression invalid?

Can someone tell me why this XPath expression is invalid?

/ReaderDetails/Reader[Pin=('2325', '2421', '2424')]

Complete code:

$dream_team_pins = implode('\', \'', array_map('trim',explode(',',get_option('dream_team_readers'))));
// Takes a comma separated list, converts to an array, and then implodes to a quoted comma separated list.
// This is currently outputting 2325', '2421', '2424 (the missing quotes at start and end are intentional)

$expression = '/ReaderDetails/Reader[Pin=(\''.$dream_team_pins.'\')]';
// I'm adding the single quotes at the start and end back in here so the entire expression is 
// outputting /ReaderDetails/Reader[Pin=('2325', '2421', '2424')]

$dream_team_readers = $readers->xpath($expression);

But I keep getting

Warning: SimpleXMLElement::xpath(): Invalid expression.

In a nutshell, I want to be able to pass several values into an xpath expression (taken from a comma separated string) to filter an XML array.

Any advice on my expression?

Upvotes: 1

Views: 564

Answers (1)

kjhughes
kjhughes

Reputation: 111726

That XPath expression uses an XPath 2.0 construct, but your XPath library only implements XPath 1.0.

Change

[Pin=('2325', '2421', '2424')]

to

[Pin='2325' or Pin='2421' or Pin='2424')]

to implement your disjunction in XPath 1.0.

Upvotes: 1

Related Questions