XPath - 選擇所選子節點的文本


鑑於我有一個以下的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應該適用於任何語言

<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 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會將此作為單個節點,因為您嘗試獲取的值不是單個節點。有沒有理由你不能這樣做?

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());

熱門答案

你想要div的任何孩子的文本不是div與類引用:

div/*[not(self::div and @class="quote")]/text()




許可下: CC-BY-SA
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因