0

Possible Duplicate:
Implementing condition in XPath and XQuery

I'm trying to write a simple script to search an XML file containing performance event information. A simplified version of the file is as follows:

<event>
    <title>Big Event 1</title>
    <tags>hip-hop</tags>
    <ticketed>YES</ticketed>
</event>
<event>
    <title>Big Event 2</title>
    <tags>jazz</tags>
    <ticketed>NO</ticketed>
</event>
<event>
    <title>Big Event 3</title>
    <tags>jazz</tags>
    <ticketed>YES</ticketed>
<event>

Using SimpleXml and Xpath I'm able to easily search any of the nodes I want. However, the problem comes when I want to search on two parameters, i.e. "give me all the events tagged jazz that are ticketed".

My idea was to pass the loaded XML through an Xpath query for each desired parameter. However, I now realise that the second query will fail, because what was returned from the first query was not a SimpleXMLObject, but an array containing SimpleXMLObjects that match.

EDIT - here's the question

How do I search the XML with multiple parameters which may or may not be present in each case. Some example queries.

  • tags contain hip-hop
  • tags contain hip-hop and is ticketed
  • title contains "3" and is not ticketed

I thought I could pass the SimpleXMLObject through an XPath query per required search parameter, but this doesn't work, because the returned value from the first XPath query is an Array, not a SimpleXmlObject.

Community
  • 1
  • 1
DaveR
  • 2,355
  • 1
  • 19
  • 36
  • you can combine "parameters" with and/or, e.g. `//event[tags="jazz" and ticketed="NO"]`, so I am not sure what the question is. Have a look at this XPath tutorial: http://schlitt.info/opensource/blog/0704_xpath.html – Gordon Oct 04 '12 at 17:23
  • Hi Gordon, I've tried to explain better. – DaveR Oct 04 '12 at 17:29
  • thanks for clarifying. It's a dupe though. You are looking for the XPath function `contains`. To have multiple conditions, use `and` and `or`. Also, make sure to check out the tutorial linked above. – Gordon Oct 04 '12 at 17:33
  • I get that. But I'm not clear about how I would build this query when I might have 6 conditions, 3 of them, 2 of them or none at all. – DaveR Oct 04 '12 at 17:39
  • 1
    I dont understand. You just write them down, e.g. `//event[contains(tags, 'hip-hop') and ticketed='YES'] and then you'd get all the ticketed hip-hop events. If that is not what you are looking for, maybe you can expand on the "which may or may not be present in each case" part. – Gordon Oct 04 '12 at 17:43
  • This is going to be a simple search application for the data contained in the XML. So the user might request lots of combinations of the various parameters. I need a way to dynamically search based on what the user's input is. – DaveR Oct 04 '12 at 17:46
  • try `'event[(tags="hip-hop" and ticketed="YES") or (contains(title, 3) and ticketed="NO")]'` – air4x Oct 04 '12 at 17:48
  • @DaveR oh, okay, you are looking for a Query Builder. – Gordon Oct 04 '12 at 18:08
  • 1
    Just so you know, a search with multiple user-supplied (or not supplied) parameters is not a "simple" search app. Anyway, show us some of the code you have so far... that will help avoid some of the guessing that Gordon had to go through. – LarsH Oct 04 '12 at 19:13

0 Answers0