1

I want to access an element using text() attribut of xpath having a structure like shown below.

<root>
    <child>
        <lowerchild>
            <lowestchild>
                My text
            </lowestchild>
        </lowerchild>
    </child>
</root>

.

//child[contains(text(), 'My text')]

should return the child-element. and

//lowerchild[contains(text(), 'My text')] 

should return the lowerchildelement.

I tried out the XPath-commands with HTMLAgilityPack, but they were not able to find those elements.

The final result of my little project is a small xpath-searcher, so the user gives the name of element the attribut and the value, so it would be great if you might give me a solution only using that information. It could be any random structure. if element names double themselves like if we had 2 lowestchild-elements, than i would like to pick the "lower" one of the lowest. Hope you can help me.

4

1 回答 1

2

代替

//child[contains(text(), 'My text')]

看起来你想要

//child[contains(., 'My text')]

XPath 表达式text()(带有隐式child::轴)选择作为上下文节点的子节点的任何文本节点。在上面的示例中,它仅选择作为child元素的直接子级的文本节点。在您展示的 XML 中,child元素有两个子文本节点,lowerchild元素位于它们之间。两个文本节点都只包含空格,因此它们可能会被某些处理器剥离,具体取决于设置。

如果您将节点集或序列作为第一个参数传递给contains(a, b),它将获取第一个节点并将其转换为字符串。因此,您的参数将转换为仅包含空格的字符串,或者是空字符串(如果仅包含空格的文本节点被剥离)。

但是,如果不是text()您将.第一个参数传递给contains(),那么上下文节点(即 a child)将被转换为字符串。这意味着连接所有文本节点后代的值child,而不仅仅是直接文本节点节点。(它有点像 DOM innerText,您的问题标题提到了它,但不包括元素的开始/结束标签,也不包括属性。)因此,//child[contains(., 'My text')]将返回child元素。

于 2013-06-27T14:32:48.160 回答