Vahid Hashemi
Vahid Hashemi

Reputation: 5240

how to write this xpath query?

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.

Upvotes: 3

Views: 643

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

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.

Upvotes: 4

user357812
user357812

Reputation:

I want to limit my result to 10 latest piece of news

/descendant::item[
   10 > last()-position()
]/*[
   self::title|self::description|self::link|self::pubDate|self::category  
]

Upvotes: 2

Treemonkey
Treemonkey

Reputation: 2163

you should try

//item[position() <= 10]/pubDate

as you want the 10th item!

Upvotes: 0

Related Questions