Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for multiple pieces of data in an XML file

Status
Not open for further replies.

Navitas

Programmer
Jul 20, 2004
24
GB
Hi,

I've done some work with XML and Xpaths before, but I'm currently stumped on the simplist way to achieve this goal!

I want to search an XML file for multiple pieces of data using the AND operator. EG.

<booking>
<customers>
<name>Fred</name>
<name>Barney</name>
<customers>
<location>
<city>London</air>
<city>France</air>
</location>
</booking>

I want to search the XML file and perform actions if the file contains the name Fred AND the city London.

Can I do this with a single xpath query that will return either a boolean value or an object containing the elements?
Thanks in advance for any help!

Darren
 
First off, your XML is not well-formed. Here is a well-formed version:

Code:
<booking>
  <customers>
    <name>Fred</name>
    <name>Barney</name>
  </customers>
  <location>
    <city>London</city>
    <city>France</city>
  </location>
</booking>

I created an little xslt that just does a simple test:

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" encoding="UTF-16" indent="yes" media-type="text/xml"/>
<xsl:template match="/">
    <xsl:if test="booking/customers[name='Fred'] and booking/location[city='London']">Straight Path</xsl:if>
    <br/>
    <xsl:if test="//customers[name='Fred'] and //location[city='London']">Slower Path</xsl:if>    
</xsl:template>
</xsl:stylesheet>

-jay
 
Thanks Jay

Sorry about the XML - I'd been swapping things around - and it looks like I completely buggered it up!!

Anyway thanks for your help - but I was trying to perform the match programmatically rather than through an xslt.

more like :

if (xmlDoc.selectSingleNode(///customers[name='Fred'] and //location[city='London'])
{
Some actions
}

Is that possible?

Cheers
Darren
 
Yes, I used DOM4J when programming through Java. DOM4J utilizes Jaxen for XPath, and I always fetched nodes utilizing XPath.

One thing you may need to think about though. If you are using a selectSingleNode, you probably want to make sure that your XPath is doing the same thing. The XPath I give you tests to see if that pattern exists. But if you using it to pull back a single node, I am not sure if it would like that XPath structure...because it is really asking for a customer node and a location node. It maybe smarter to design the XPath to resolve to one node like:

"//booking[*/name='Fred' and */city='London']"

This will say that search for any booking elements that contain name='Fred' and city='London' as grandchildren...more or less.

-jay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top