仅在儿童节点中搜索XPATH搜索



我正在使用HTML敏捷包来从节点中提取文本。

            var sb = new StringBuilder();
            foreach (HtmlNode innernode in node.SelectNodes("//*[not(self::script or self::style)]/text()[not(normalize-space(.)='')]"))
            {
                sb.Append(innernode.InnerText);
            }
            Console.WriteLine(sb.ToString());

我正在使用此代码。我想从" node"中提取文本,并且是子节点,但是此XPath查询返回来自整个HTML文档(我猜它从根节点开始搜索)。我知道这很愚蠢,但是我该如何更新xpath,以便它仅在" node"的子节点中搜索:)

谢谢

要包括node的孩子(我也假设所有其他后代)的文本节点,以及node的文本节点,您可能想要:

./descendant-or-self::*[not(self::script or self::style)]/text()[not(normalize-space(.)='')]

.//*[not(self::script or self::style)]/text()[not(normalize-space(.)='')]将不包括node的直接儿童文本节点,因为这意味着./descendant-or-self::*/*[not(self::script or self::style)]...

最新更新