6

I am trying to build a simple search-engine using HtmlAgilityPack and Xpath with C# (.NET 4). I want to find every node containing a userdefined searchword, but I can't seem to get the XPath right. For Example:

<HTML>
 <BODY>
  <H1>Mr T for president</H1>
   <div>We believe the new president should be</div>
   <div>the awsome Mr T</div>
   <div>
    <H2>Mr T replies:</H2>
     <p>I pity the fool who doesn't vote</p>
     <p>for Mr T</p>
   </div>
  </BODY>
</HTML>

If the specified searchword is "Mr T" I'd want the following nodes: <H1>, The second <div>, <H2> and the second <p>. I have tried numerous variants of doc.DocumentNode.SelectNodes("//text()[contains(., "+ searchword +")]"); but I always seem to wind up with every single node in the entire DOM.

Any hints to get me in the right direction would be very appreciated.

4 Answers 4

12

Use:

//*[text()[contains(., 'Mr T')]]

This selects all elements in the XML document that have a text-node child which contains the string 'Mr T'.

This can also be written shorter as:

//text()[contains(., 'Mr T')]/..

This selects the parent(s) of any text node that contains the string 'Mr T'.

5
  • Your second expression matches exactly what the OP says he tried (except for the additional ..), so I'm not sure why he thinks it didn't work.
    – Wayne
    Jan 20, 2012 at 23:35
  • @lwburk: It "didn't work, because it selects text nodes and he needs their parents. Jan 20, 2012 at 23:45
  • Right, I get that, but it certainly doesn't select "every single node in the entire DOM" (as claimed).
    – Wayne
    Jan 20, 2012 at 23:49
  • OK, it turned out that I didn't do my homework properly... it wasn't the query itself that was the problem, but the way I coded the query! When I wrote ..."+ searchword +"... I actually got every node in the DOM... but when I tried ...'"+ searchword +"'... I got it right. But I wouldn't have got it if it wasn't for you guys! I actually tried my mock xml and got it right when I hardcoded the question just like Dimitres example, but when I did the same thing with the stringbased searchword, everythin else the same, I got all of the nodes again... Thank you very much! Jan 22, 2012 at 19:46
  • That sure is some wonky expression there, but copy-paste replace 'Mr T' with 'My text' and it did exactly what i needed! Oct 1, 2014 at 14:33
2

According to Xpath, if you want to find a specific keyword you need to follow the format ("keyword" is the word you like to search) :

//*[text()[contains(., 'keyword')]]

You have to follow the same format as above in C#, keyword is the string variable you call:

doc.DocumentNode.SelectNodes("//*[text()[contains(., '" + keyword + "')]]");
2
  • Please try explaining your answer with more detail. This will not only help the OP, but anyone else in the future that may have the same problem. Dec 9, 2016 at 18:16
  • It's sensitive to small or capital alphabets. How can one ignore case sensitivity?
    – x19
    Feb 26, 2021 at 12:54
0

Use the following:

doc.DocumentNode.SelectNodes("//*[contains(text()[1], " + searchword + ")]")

This selects all elements (*) whose first text child (text()[1]) contains the searchword.

0

Case-insensitive solution:

var xpathForFindText = "//*[text()[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '" + lowerFocusKwd + "')]]";

var result=doc.DocumentNode.SelectNodes(xpathForFindText);

Note:

Be careful, because the lowerFocusKwd must not contain the following character, because the xpath will be in bad format:

'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.