如何使用XPath获取节点值/innerHTML? [英] How to get node value / innerHTML with XPath?

查看:97
本文介绍了如何使用XPath获取节点值/innerHTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XPath可以选择我想要的类://div[@class='myclass'].但这会返回整个div(也带有<div class='myclass'>,但是我只想返回此标签的内容,而没有标签本身.我该怎么做?

I have a XPath to select to a class I want: //div[@class='myclass']. But it returns me the whole div (with the <div class='myclass'> also, but I would like to return only the contents of this tag without the tag itself. How can I do it?

推荐答案

使用xpath,您将获得的返回值是路径中最后一个不是条件的值.那意味着什么?好吧,条件是介于[](但您已经知道)之间的东西,而您的读取内容类似于 pathElement [",它具有值为"my class"的"class"属性] . pathElement直接位于[之前.

With xpath, the thing you will get returned is the last thing in the path that is not a condition. What that means? Well, conditions are the stuff between []'s (but you already knew that) and yours reads like pathElement[that has a 'class' attribute with value 'my class']. The pathElement comes directly before the [.

[]之外的所有东西都是路径,所以在//a/b/c[@blah='bleh']/d a b c d 都是路径元素, blah 是属性, bleh 是文字值.如果此路径匹配,它将返回 d ,这是最后一个无条件的东西.

All the stuff outside of []'s then is the path, so in //a/b/c[@blah='bleh']/d a, b, c and d are all path elements, blah is an attribute and bleh a literal value. If this path matches it will return you a d, the last non-condition thing.

您的特定路径将返回(一系列) div ,这是xpath路径中的最后一件事.因此,此返回值包括顶层节点(在您的情况下为 div ),并在其下(所有)其(所有)子节点.节点可以是元素或文本(或注释,处理指令等).

Your particular path returns a (series of) div, being the last thing in your xpath's path. This return value thus includes the top-level node(s), div in your case, and underneath it (them) all its (their) children. Nodes can be elements or text (or comments, processing instructions, ...).

在一个节点下可以有多个文本节点,因此数组pOcHa涉及. x/text()返回所有x的直接子文本,x/node()返回所有子节点,包括 文本.

Underneath a node there can be multiple text nodes, hence the array pOcHa talks about. x/text() returns all text that is a direct child of x, x/node() returns all child nodes, including text.

希望这会有所帮助.

这篇关于如何使用XPath获取节点值/innerHTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆