Marc
Marc

Reputation: 559

Xpath query contains

I have the folowing code:

$xml = simplexml_load_file($url);

$query = '/root/parent[Model="Corolla"]';

//RUN QUERY ON XML
$xQuery = $xml->xpath($query);

And here is the XML file:

<root>
   <parent>
     <Model>Corolla A/C</Model>
   </parent>
</root>

This code works good if I'm searching for the exact string "Corolla". The problem I'm having is that some of the values are similar to "Corolla A/C good condition"

Is there a way I can query the XML file for any Model element that contains the word Corolla rather then the exact string Corolla?

EDIT: I added an example of my XML help clarify.

Upvotes: 1

Views: 3829

Answers (2)

aefxx
aefxx

Reputation: 25249

In this particular case I would suggest the starts-with function rather than contains (given the assumption all your models begin with the word "Corolla"):

EDIT

Based on your edit, you would have to change the query to:

/root/parent/Model[starts-with(., 'Corolla')]

You may also have a look at the Zvon XSLT Tutorial. It's a very nice read.

Upvotes: 3

Marc B
Marc B

Reputation: 360562

/root/parent[contains(@Model, 'Corolla')]

Note the @ on the Model... that's necessary to indicate that you're working with an attribute of the parent tag, and not some OTHER tag whose name happens to be Model.

Upvotes: 1

Related Questions