如何通过XPath提取属性节点的值?

一个示例XML文件是:

<parents name='Parents'>
  <Parent id='1' name='Parent_1'>
    <Children name='Children'>
      <child name='Child_2' id='2'>child2_Parent_1</child>
      <child name='Child_4' id='4'>child4_Parent_1</child>
      <child name='Child_1' id='3'>child1_Parent_1</child>
      <child name='Child_3' id='1'>child3_Parent_1</child>
    </Children>
  </Parent>
  <Parent id='2' name='Parent_2'>
    <Children name='Children'>
      <child name='Child_1' id='8'>child1_parent2</child>
      <child name='Child_2' id='7'>child2_parent2</child>
      <child name='Child_4' id='6'>child4_parent2</child>
      <child name='Child_3' id='5'>child3_parent2</child>
    </Children>
  </Parent>
</parents>

到目前为止,我有这个XPath字符串:

//Parent[@id='1']/Children/child[@name]  

它只返回子元素,但我希望有name属性的值。

对于我的示例XML文件,下面是我希望输出的内容:

Child_2
Child_4
Child_1
Child_3

我有一个小问题,XPath包含与dom4j…

假设我的XML是

<Home>
    <Addr>
        <Street>ABC</Street>
        <Number>5</Number>
        <Comment>BLAH BLAH BLAH <br/><br/>ABC</Comment>
    </Addr>
</Home>

假设我想找到文本中所有有ABC的节点,给定根元素…

所以我需要写的XPath是

/ * [contains(短信),‘ABC’)

然而,这不是dom4j返回的内容....这是dom4j的问题,还是我对XPath工作原理的理解,因为该查询只返回Street元素而不返回Comment元素?

DOM使Comment元素成为一个具有四个标记(两个)的复合元素

[Text = 'XYZ'][BR][BR][Text = 'ABC'] 

我假设查询仍然应该返回元素,因为它应该找到元素并在其上运行contains,但它没有……

下面的查询返回元素,但它返回的不仅仅是元素——它还返回父元素,这对问题来说是不可取的。

//*[contains(text(),'ABC')]

有人知道XPath查询只返回元素<Street/>和<Comment/>吗?

XPath bookstore/book[1]选择书店下的第一个图书节点。

如何选择第一个匹配更复杂条件的节点,例如,第一个匹配/bookstore/book[@location='US']的节点

我想找的是:

getElementByXpath(//html[1]/body[1]/div[1]).innerHTML

我需要得到元素的innerHTML使用JS(使用硒WebDriver/Java,因为WebDriver不能找到它本身),但如何?

我可以使用ID属性,但不是所有元素都有ID属性。

(固定)

我使用jsoup在Java中完成它。这符合我的需要。

给定一个这样的XML结构:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>

我怎么能得到的值的lang(其中lang是eng在书名),为第一个元素?

在我的网页中,有一个div,类名为Test。

如何用XPath找到它?

当属性包含多个单词时,我有一个按属性选择节点的问题。例如:

<div class="atag btag" />

这是我的xpath表达式:

/ / * [@class =’atag’]

这个表达式适用于

<div class=“atag” />

但对于前面的例子不是这样。我如何选择<div>?