How to Check for Multiple Conditions in Xpath

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

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()"; 

回答1:

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).



回答2:

try

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


回答3:

/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



回答4:

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'] 


回答5:

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



回答6:

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'] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!