0

I have a xml file

<events date="30/08/2010">
<event>
<title>here</title>
<description>
This is first Event This is first Event This is first Event This is first Event This is first Event This is first Event This is first Event This is first Event This is first Event This is first Event
</description>
</event>
</events>
<events date="31/08/2010">
<event>
<title>Second Event </title>
<description>
Second Event Second Event Second Event Second Event Second Event Second Event Second Event Second Event Second Event Second Event
</description>
</event>
</events>

from this xml how i can select the event with title Second Event using xquery.I used

$nodes = $xml->xpath('//xml/events/event[@title="'.$title.'"]');

but it is not working, can anybody help me

2
  • Take a look to w3schools.com/xpath/xpath_syntax.asp, this should help you understand XPath syntax better. Commented Aug 30, 2010 at 7:50
  • It's probably not a good idea to name an element "xml". Quoting from section 2.3 of XML 1.0: "Names beginning with the string "xml", or with any string which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for standardization in this or future versions of this specification." Commented Aug 30, 2010 at 8:32

2 Answers 2

2

You don't have a node called "xml" so your query should not start "xml". Title is not an attribute, so remove the "@". This (untested) ought to work:

//events/event[title="'.$title.'"]'

and return a node list.

Sign up to request clarification or add additional context in comments.

2 Comments

My xml is start with xml thats y i used .I tried this one then also it is not working
@THOmas the above works fine for me with the XML you show in your question. Just make sure you use Second Event with the trailing space to find the node or switch to /events/event[normalize-space(title)="Second Event"] or - better - /events/event[contains(title, "Second Event")]
0
$nodes = $xml->xpath('//xml/events/event[title="'.$title.'"]');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.