xpath获取包含文本的节点

时间:2022-11-30 22:18:04

I tried to search for nodes containing text 'Yahoo' under '/doc/story/content', it returns 'content' node, but I need exact text node that contains 'Yahoo' or it's parent

我尝试在'/doc/story/content'下搜索包含'Yahoo'的节点,它返回'content'节点,但我需要包含'Yahoo'或它的父节点的文本节点

<doc>
    <story>
        <content id="201009281450332423">
            <ul>MSW NYNES NYPG1 DILMA</ul>
            <p> <k> Yahoo, made </k> it nice </p>
            <p>
               <author>-v-</author>
            </p>
        </content>
    </story>
</doc>

Xpath: "/doc/story/content[contains(., 'Yahoo')]"

Xpath:/ doc /故事/[包含的内容。“雅虎”)”

2 个解决方案

#1


38  

Your XML is malformed. </content></doc></story> should be </content></story></doc>.

XML是畸形的。< /内容> < / doc > < /故事>应该是< /内容> < /故事> < / doc >。

Apart from that, the XPath you would want is

除此之外,您需要的XPath是

/doc/story/content//*[contains(., 'Yahoo')]

(select any descendant of <content> which contains the text "Yahoo" -- this will select the <p>)

(选择任何包含“Yahoo”文本的 的后代——这将选择

)

#2


34  

Since you need all textNodes only which contain the text Yahoo, use the following XPath.

由于只需要包含文本Yahoo的所有文本节点,请使用以下XPath。

//text()[contains(., 'Yahoo')]

This should return you all the textNodes only which contains Yahoo (case-sensitive) in it.

这将只返回包含Yahoo(区分大小写)的所有textnode。

#1


38  

Your XML is malformed. </content></doc></story> should be </content></story></doc>.

XML是畸形的。< /内容> < / doc > < /故事>应该是< /内容> < /故事> < / doc >。

Apart from that, the XPath you would want is

除此之外,您需要的XPath是

/doc/story/content//*[contains(., 'Yahoo')]

(select any descendant of <content> which contains the text "Yahoo" -- this will select the <p>)

(选择任何包含“Yahoo”文本的 的后代——这将选择

)

#2


34  

Since you need all textNodes only which contain the text Yahoo, use the following XPath.

由于只需要包含文本Yahoo的所有文本节点,请使用以下XPath。

//text()[contains(., 'Yahoo')]

This should return you all the textNodes only which contains Yahoo (case-sensitive) in it.

这将只返回包含Yahoo(区分大小写)的所有textnode。