Xpath查询帮助

时间:2011-06-21 22:17:29

标签: xml xpath

在Xpath中如何查询我是否正在寻找孩子的年龄和姓名谁没有孙子

 <documentRoot>
 <parent name="data" >

  <child id="1"  name="alpha" >
   <anotherchild>
     <age>20</age>
     <grandchild/>
   </anotherchild>
  </child>

  <child id="2"  name="beta" >
   <anotherchild>
     <age>50</age>
     <grandchild id="2.1"  name="beta-alpha" ></grandchild>
     <grandchild id="2.2"  name="beta-beta" ></grandchild>
   </anotherchild>
  </child>

 </parent>
</documentRoot>

1 个答案:

答案 0 :(得分:3)

看起来你的XML有一些拼写错误。假设您的XML是这样的(在<grandchild/>下面</grandchild>而不是<child>,并且结束</documentRoot>

<documentRoot>
    <parent name="data">
        <child id="1" name="alpha">
            <age>20</age>
            <grandchild/>
        </child>
        <child id="2" name="beta">
            <age>50</age>
            <grandchild id="2.1" name="beta-alpha"/>
            <grandchild id="2.2" name="beta-beta"/>
        </child>
    </parent>
</documentRoot>

此XPath将选择<grandchild/>存在的子元素的年龄,而不包含@id

//child[not(anotherchild/grandchild/@id)]/anotherchild/age

这将返回相同child

的名称
//child[not(anotherchild/grandchild/@id)]/anotherchild/@name