How to extract a node based on content in XPATH?
The URL i am trying to extract the main body text is
indiainfoline"DOT"com/Markets/News/Cement-shares-build-on-budget-expectations/3567698750
Below is the text string that i am seraching for --
"UltraTech开发者_C百科 Cement (up 2.77%), Ambuja Cements (up 2.15%), ACC (up 1.72%), India Cements (up 1.50%), Madras Cement (up 1.68%), Prism Cement (up 1.81%), JK Lakshmi Cements (up 2.11%) and Shree Cement"
XPATH query i am using is
sampleBody = hd.DocumentNode.SelectSingleNode("
//*[contains(.,'UltraTech Cement (up 2.77%)')]").InnerText;
I am getting the entire page instead of just the text of that particular node.
Can any one please suggest the reason?
Thank you.
I am getting the entire page instead of just the text of that particular node.
Can any one please suggest the reason?
This XPath expression:
//*[contains(.,'UltraTech Cement (up 2.77%)')]
It means: any child element having 'UltraTech Cement (up 2.77%)'
as part of its string value. This is because .
would be expanded to self::node()
an then cast with string()
as argument for contains()
.
So, if there is such string in the document content, the root element will have it as part of its string value.
If you need the innermost element having such string value, use this XPath expression:
//*[contains(.,'UltraTech Cement (up 2.77%)')]
[not(*[contains(.,'UltraTech Cement (up 2.77%)')])]
Colloquial Meaning: I have the string and none of my children does.
精彩评论