选择的 xpath 父属性 [英] xpath parent attribute of selection

查看:44
本文介绍了选择的 xpath 父属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xml 文档的语法:

<x name="GET-THIS">
  <y>
    <z>Z</z> 
    <z>Z__2</z>
    <z>Z__3</z>   
  </y>
</x>

我可以使用以下方法获取所有 z 元素:

I'm able to get all z elements using:

xpath("//z")

但在那之后我被卡住了,我不知道下一步该怎么做.我不太明白 .. 父方法的语法

But after that I got stuck, I'm not sure what to do next. I don't really understand the syntax of the .. parent method

那么,如何获取元素的父元素的父元素的属性呢?

So, how do I get the attribute of the parent of the parent of the element?

推荐答案

与其遍历父级,只需找到合适的父级开始:

Instead of traversing back to the parent, just find the right parent to begin with:

  • //x 将选择所有 x 元素.
  • //x[//z] 将选择所有具有 z 元素作为后代的 x 元素.
  • //x[//z]/@name 将获得每个元素的 name 属性.
  • //x will select all x elements.
  • //x[//z] will select all x elements which have z elements as descendants.
  • //x[//z]/@name will get the name attribute of each of those elements.

这篇关于选择的 xpath 父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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