java - How to Check for Multiple Conditions in Xpath -


i want retrive pname of row/field id =2 , 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' , ./text()]"; 

edit:

i pname whoese id 2 , paddress=india

string expression="/mysqldump/database[@name='mydb']/table[@name='descriptiontable']/row/field[@name='id' , .='2']and[@name='paddress' , .='india']/../field[@name='pname']/text()"; 

both of above answers improved moving aspects of path expression predicates, , using nested nested predicates. imho makes xpath selection more human readable.

first find row field @name eq id , text() = "2", there can select field row @name eq "pname".

/mysqldump/database[@name = "mydb"]/table[@name = "descriptiontable"]/row[field[@name eq "id"][text() = "2"]]/field[@name = "pname"] 

also note explicit use of eq , =, eq used comparing atomic values, in instance selection of our attributes, , = used comparing sequences (as conceivable text() may return more 1 item - although won't example xml).


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -