Friday, 15 July 2011

xpath - Selecting an element whose sibling contains an attribute XML -



xpath - Selecting an element whose sibling contains an attribute XML -

i'm trying select title value attribute value greater 1:

path="//book/price[@value >1]

i'm not sure how select titles attribute value element cost greater one. i'm xml beginner , i'm doing few exercises tutorial.

my priorities are

learning xml super xml

xml:

<bookstore> <book> <title lang="en">harry potter</title> <price value="1">49.99</price> </book> <book> <title lang="en">learning xml</title> <price value="2">29.95</price> </book> <book> <title lang="en">super xml</title> <price value="3">39.95</price> </book> </bookstore>

update

the first query perfect show me how obtain nested element?

<bookstore> <book> <title lang="en">harry potter</title> <possibleprice> <price value="1">49.99</price> </possibleprice> </book> <book> <title lang="en">learning xml</title> <possibleprice> <price value="2">29.95</price> </possibleprice> </book> <book> <title lang="en">super xml</title> <possibleprice> <price value="3">39.95</price> </possibleprice> </book> </bookstore>

for example:

for first version of op:

input:

<bookstore> <book> <title lang="en">harry potter</title> <price value="1">49.99</price> </book> <book> <title lang="en">learning xml</title> <price value="2">29.95</price> </book> <book> <title lang="en">super xml</title> <price value="3">39.95</price> </book> </bookstore>

xpath:

//book/price[@value > 1]/preceding-sibling::title

result:

<title lang="en">learning xml</title> <title lang="en">super xml</title>

for updated sec xml version of op, different example:

xpath:

//book/possibleprice/price[@value > 1]/ancestor::book/title

same result above.

to reply question in comment if necessary utilize preceding-sibling twice 2nd version: no. preceding-sibling targets preceding-sibling of current node. current node in case price , has no siblings, parent possibleprice. same result in different way given above, possible parent of price , preceding-sibling of possibleprice:

//book/possibleprice/price[@value > 1]/parent::possibleprice/preceding-sibling::title

which results in same.

as mentioned op beginner in xml, maybe next can useful illustrate xpath axes: http://www.xmlplease.com/axis

xml xpath

No comments:

Post a Comment