Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write this xpath query?

Tags:

xml

rss

xpath

I'm cosuming rss from several sites and my real problem is with their Pubdate field because most of their PubDate values are not valid somehow I manage to retrieve the value from the PubDate fieldset with the help of xpath. this is what I've written :

//item/title | 
//item/description | 
//item/link | 
//item/pubDate | 
//item/category

and I want to limit my result to 10 latest piece of news I know in xpath we have a function called postion() and I have to use it like following :

[postion() <= 10]

but when I mix these two xpath queries into together I won't get proper result :

 //item/title | 
 //item/description | 
 //item/link | 
 //item/pubDate | 
 //item/category [position() <= 10]

how can I write this particular xpath query in correct format. and is there any fast-track book for xpath around?

regads.

like image 511
Seyed Vahid Hashemi Avatar asked Mar 09 '26 04:03

Seyed Vahid Hashemi


1 Answers

I assume that the latest news are at the top.

Use:

(//item)[not(position() > 10)]/*
           [self::title or self::description 
           or self::link or self::pubDate or self::category
           ]

Explanation:

This expression selects all title, description, link, pubDate and category elements that are children of one of the first 10 item elements in the XML document.

It is a FAQ and an often commited mistake to try selecting the first (or any position element, say item) by:

//item[1]

This selects all item elements in the document that are the first child of their parent -- and there may be many such item elements.

The XPath expression that selects just the first item element in the document is:

(//item)[1]

Rule to remember: The [] operator has higher precedence (binds stronger) than the // abbreviation.

like image 155
Dimitre Novatchev Avatar answered Mar 11 '26 16:03

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!