1.

Different syntax for XPath Query

Answer»

Below are some usefull and more useable syntax in xpath all of these syntax are usefull when we are doing in xml

/catalog SELECTS the root element
/catalog/cdselects all the cd elements of the catalog element
/catalog/cd/price selects all the price elements ofall the cd elements of the catalog element
/catalog/cd[price>10.0]selects all the cd elements with price greater than 10.0
startswith a slash(/)representsan absolute path to an element
startswith TWO SLASHES(//)selectsall elements that satisfy the criteria
//cdselects all cd elements in the document
/catalog/cd/title| /catalog/cd/artist selects all the title and artist elements of the cd elements of catalog
//title| //artistselects all the title and artist elements in the document
/catalog/cd/*selects all the CHILD elements of all cd elements of the catalog element
/catalog/*/priceselectsall the price elements that are grandchildren of catalog
/*/*/priceselects all price elements which have two ancestors
//* selectsall elements in the document
/catalog/cd[1]selectsthe first cd child of catalog
/catalog/cd[last()] selectsthe last cd child of catalog
/catalog/cd[price]selects all the cd elements that have price
/catalog/cd[price=10.90]selects cd elements with the price of 10.90
/catalog/cd[price=10.90]/priceselects all price elements with the price of 10.90
//country selects all countryattributes
//cd[country]selects cd elements which have a " country " ATTRIBUTE
//cd[*]selects cdelements which have any attribute
//cd[country='UK']selects cd elements with " country " attribute equal to 'UK'


Discussion

No Comment Found