XPath-选择所选子节点的文本 [英] XPath - select text of selected child nodes
问题描述
鉴于我有以下xml:
Given that I have a following xml:
<div id="Main">
<div class="quote">
This is a quote and I don't want this text
</div>
<p>
This is content.
</p>
<p>
This is also content and I want both of them
</p>
</div>
是否存在"XPath" ,以帮助我选择 div#Main 的内部文本作为单个节点,但必须排除任何 div.quote .
Is there "a XPath" to help me select inner text of div#Main as a single node, but must exclude texts of any div.quote.
我只想输入以下文字:这很满足.这也很满足,而且我俩都想要"
I just want the text: "This is content.This is also content and I want both of them"
预先感谢
这是测试XPath的代码,我将HtmlAgilityPack与.NET结合使用,但我相信xPath应该可以与任何语言一起使用
Here is the code to test the XPath, I'm using .NET with HtmlAgilityPack but I believe the xPath should work with any languages
[Test]
public void TestSelectNode()
{
// Arrange
var html = "<div id=\"Main\"><div class=\"quote\">This is a quote and I don't want this text</div><p>This is content.</p><p>This is also content and I want both of them</p></div>";
var xPath = "//div/*[not(self::div and @class=\"quote\")]/text()";
var doc = new HtmlDocument();
doc.LoadHtml(html);
// Action
var node = doc.DocumentNode.SelectSingleNode(xPath);
// Assert
Assert.AreEqual("This is content.This is also content and I want both of them", node.InnerText);
}
由于xPath仍然不正确,测试显然失败了.
The test was failed obviously because the xPath is still not correct.
Test 'XPathExperiments/TestSelectNode' failed:
Expected values to be equal.
Expected Value : "This is content.This is also content and I want both of them"
Actual Value : "This is content."
推荐答案
我不认为有一个XPath可以将其作为单个节点提供,因为您正在尝试获取 aren'的值一个节点.您有理由不能这样做吗?
I don't think there is an XPath that will give you this as a single node, because the values you're trying to obtain aren't a single node. Is there a reason you can't do this?
StringBuilder sb = new StringBuilder();
// Action
var nodes = doc.DocumentNode.SelectNodes(xPath);
foreach(var node in nodes)
{
sb.Append(node.InnerText);
}
// Assert
Assert.AreEqual("This is content.This is also content and I want both of them",
sb.ToString());
这篇关于XPath-选择所选子节点的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!