canora
canora

Reputation: 23

XPath : How to get concatenated text of two sibling nodes?

I would like to know how to get concatenated text of two sibling nodes.

This is my code.

string html =
    "<html>" +
    "   <div class='abc'>" +
    "       <h3><a href='def'>ghi</a></h3>" +
    "       <div>text1</div>" +
    "       <div>text2</div>" +
    "   </div>" +
    "   <div class='abc'>" +
    "       <h3><a href='jkl'>mno</a></h3>" +
    "       <div>text3</div>" +
    "       <div>text4</div>" +
    "   </div>" +
    "</html>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='abc']");
HtmlNodeCollection nodes2, nodes3;
foreach (HtmlNode node in nodes)
{
    nodes2 = node.SelectNodes(".//h3/a");
    nodes3 = node.SelectNodes("?????????????");
}

I want to get result

text1text2

and then

text3text4

How do I write a query replace of question marks? I know I can get text with iterating through nodes using foreach. But I must do this with XPath query.

Thanks.

Upvotes: 0

Views: 768

Answers (3)

canora
canora

Reputation: 23

@Mathias Müller 's comment is answer.

SelectNodes returns node list and we must navigate through collection nodes with C# programming.

The mistake is that I'm waiting text result.

Upvotes: 0

Helmer
Helmer

Reputation: 439

concat(//div[@class='abc'][1]/div[1]/text(), //div[@class='abc'][1]/div[2]/text())

should give: text1text2

concat(//div[@class='abc'][2]/div[1]/text(), //div[@class='abc'][2]/div[2]/text())

should give text3text4

This should work with XPATH 1.0 which is used in htmlagilitypack.

Upvotes: 2

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

I'm not sure I understand, but given that an outer div element is your context, the following expression:

concat(div[1],div[2])

would return the concatenation of the string values of the first and second child div elements.


By the way, h3 is an immediate child of /html/div, so there is no need to use // in this expression:

nodes2 = node.SelectNodes(".//h3/a");

You might want to reduce it to

nodes2 = node.SelectNodes("h3/a");

Upvotes: 1

Related Questions