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]
开发者_JS百科how can I write this particular xpath query in correct format. and is there any fast-track book for xpath around?
regads.
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.
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
]
you should try
//item[position() <= 10]/pubDate
as you want the 10th item!
精彩评论