我不知道为什么以下查询语句无法运行:
//a/@href[@class='specified_string']
//a/@href[@class='specified_string']
尝试另一种方式:
//a[@class='specified_string']/@href
毕竟,class
是<a>
元素的属性,而不是href
属性的属性。
NodeList
应该包含代表 href
属性的 Node
列表。因此,只需在这些节点上运行 getNodeValue()
就可以获得 href
属性的值。 - ndim//a[@name="topic"]
来提取正确的a元素。但是,//a[@name="topic"]/@href
返回一个列表[, , , , , ]
,好像没有href属性一样。这是一个示例"a元素":<a href="/topic/19778287" name="topic">XXX</a>。 - Skywalker326属性不能有属性,只有元素可以有属性。
原始的XPath表达式:
//a/@href[@class='specified_string']
选择任何 a
元素的任何 href
属性,使其具有一个属性 class
,其值为 'specified_string'
。
你想要的是:
//a[@class='specified_string']/@href
href
的属性,该属性(这就是错误所在)的 class
应该等于 specified_string
。a
的属性 class
为 specified_string
的 href
属性。在锚点标签中没有class属性,只有href。可以使用//*[@href='value']
来识别,但是//*a[@href='value']
无法正常工作。
//a[contains(concat(' ',normalize-space(@class),' '), 'some_class_name')]/@href
。 - singpolyma' some_class_name '
(周围有空格),不是'some_class_name'
。 - kmkaplan