鉴于我有以下 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的文本。
我只想要文字:“这是内容。这也是内容,我想要它们两个”
提前致谢
这是测试 XPath 的代码,我使用 .NET 和 HtmlAgilityPack 但我相信 xPath 应该适用于任何语言
[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 仍然不正确。
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."