Code Hungry
Code Hungry

Reputation: 4000

How to Check for Multiple Conditions in Xpath

I want to retrive PName of the row/field whose id =2 and pAddress=INDIA

<?xml version="1.0"?>
<mysqldump >
<database name="MyDb">
<table name="DescriptionTable">
<row>
<field name="id">1</field>
<field name="pName">XYZ</field>
<field name="pAddress">INDIA</field>
<field name="pMobile">1234567897</field>
</row>
<row>
<field name="id">2</field>
<field name="pName">PQR</field>
<field name="pAddress">UK</field>
<field name="pMobile">755377</field>
</row>
<row>
<field name="id">3</field>
<field name="pName">ABC</field>
<field name="pAddress">USA</field>
<field name="pMobile">67856697</field>
</row>
</table>
</database>
</mysqldump>

String expression="/mysqldump/database[@name='MyDb']/table[@name='DescriptionTable']/row/field[@name='id' and ./text()]";

Edit:

I would like to get Pname whoese id is 2 and pAddress=INDIA

String expression="/mysqldump/database[@name='MyDb']/table[@name='DescriptionTable']/row/field[@name='id' and .='2']and[@name='pAddress' and .='INDIA']/../field[@name='pName']/text()";

Upvotes: 2

Views: 2458

Answers (6)

ievche
ievche

Reputation: 1815

As I understand, you want to get text from two pNames.

I think this should work for you in scope of current xml:

//row/field[text()='2' OR text()='INDIA']/../field[@name='pName']/text()

If you want to take just nodes:

//row/field[text()='2' OR text()='INDIA']/../field[@name='pName']

Upvotes: 0

SAM
SAM

Reputation: 47

The Simplest way to achieve the above is

String expression = "/mysqldump/database[@name='MyDb']/table[@name='DescriptionTable']/
    row[./field[@name="id"]/text()="2" and ./field[@name="pAddress"]/text()="INDIA"]
/field[@name="pName"]/text()";

You can add multiple conditions in the second line seperated by and/or based on your needs

Upvotes: 0

har07
har07

Reputation: 89325

You can try to do this way :

/mysqldump
/database[@name='MyDb']
/table[@name='DescriptionTable']
/row[
        field[@name[.='id'] and .=2] 
            and 
        field[@name[.='pAddress'] and .='INDIA']
    ]
/field[@name='pName']

Above XPath will select <row> element whose id is 2 and pAddress=INDIA, then get the row's pName. But looking at the sample XML in this question, there is no such <row> that fulfill both criteria. If you meant to select row which either has id equals 2 or has pAddress equals INDIA, you can use or instead of and in the row filtering expression :

/mysqldump
/database[@name='MyDb']
/table[@name='DescriptionTable']
/row[
        field[@name[.='id'] and .=2] 
            or 
        field[@name[.='pAddress'] and .='INDIA']
    ]
/field[@name='pName']

Upvotes: 0

adamretter
adamretter

Reputation: 3517

Both of the above answers could be improved by moving aspects of the path expression into the predicates, and using nested nested predicates. IMHO this makes the XPath selection much more human readable.

First we find the row with the field whose @name eq id and text() = "2", from there we can simply select the field from that row whose the @name eq "pName".

/mysqldump/database[@name = "MyDb"]/table[@name = "DescriptionTable"]/row[field[@name eq "id"][text() = "2"]]/field[@name = "pName"]

Also note the explicit use of eq and =, eq is used for comparing atomic values, in this instance the selection of our attributes, and = is used for comparing sequences (as it is conceivable that text() may return more than one item - although it won't for your example XML).

Upvotes: 2

Chamil
Chamil

Reputation: 803

/mysqldump/database/table/row/field[@name='id' and .=2]/../field[@name='pName']

Explanation:

/mysqldump/database/table/row/field[@name='id' and .=2]

gets the field where id name attribute= id and the value equals 2

../

goes to the parent node.

field[@name='pName']

searches the field where attribute name contains pName

Upvotes: 0

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

try

/mysqldump/database[@name='MyDb']/table[@name='DescriptionTable']/row/field[@name='id'][.='2']/following-sibling::field[@name='pName']/text()

Upvotes: 0

Related Questions