• [XPath] XPath 与 lxml (三)XPath 坐标轴


    本章我们将沿用上一章的 XML 示例文档。

    XPath 坐标轴

    坐标轴用于定义当对当前节点的节点集合。

    坐标轴名称 含义
    ancestor 选取当前节点的所有先辈元素及根节点。
    ancestor-or-self 选取当前节点的所有先辈以及当前节点本身。
    attibute 选取当前节点的所有属性。
    child 选取当前节点的所有子元素。
    descendant 选取当前节点的所有后代元素。
    descendant-or-self 选取当前节点的所有后代元素以及当前节点本身。
    following 选取文档中当前节点的结束标签之后的所有节点。
    following-sibling 选取当前节点之后的所有同级节点
    namespace 选取当前节点的所有命名空间节点。
    parent 选取当前节点的父节点。
    preceding 选取当前节点的开始标签之前的所有节点。
    preceding-sibling 选取当前节点之前的所有同级节点。
    self 选取当前节点。

     

    位置路径表达式

    位置路径可以是绝对路径,也可以是相对路径。绝对路径以 "/" 开头。每条路径包括一个或多个步,每步之间以 "/" 分隔。

    绝对路径:/step/step/...

    相对路径:step/step/...

    每步根据当前节点集合中的节点计算。

    步(step)包括三部分:

    • 坐标轴(axis):定义所选节点与当前节点之间的关系。
    • 节点测试(node-test):识别某个坐标轴内部的节点。
    • 预判(predicate):提出预判条件对节点集合进行筛选。

    步的语法:

    坐标轴::节点测试[预判]

    实例

    # child::nodename 选取所有属于当前节点的 book 子元素,等价于 './nodename'
    >>> root.xpath('child::book')
    [<Element book at 0x2d888c8>, <Element book at 0x2d88878>]
    >>> root.xpath('./book')
    [<Element book at 0x2d888c8>, <Element book at 0x2d88878>]
    
    # attribute::lang 选取当前节点的 lang 属性,等价于 './@lang'
    >>> root.xpath('//*[@lang]')[0].xpath('attribute::lang')
    ['eng']
    >>> root.xpath('//*[@lang]')[0].xpath('@lang')
    ['eng']
    
    # child::* 选取当前节点的所有子元素,等价于 './*'
    >>> root.xpath('child::*')
    [<Element book at 0x2d88878>, <Element book at 0x2d88738>]
    >>> root.xpath('./*')
    [<Element book at 0x2d88878>, <Element book at 0x2d88738>]
    
    # attribute::* 选取当前节点的所有属性,等价于 './@*'
    >>> root.xpath('//*[@*]')[0].xpath('attribute::*')
    ['eng']
    >>> root.xpath('//*[@*]')[0].xpath('@*')
    ['eng']
    
    # child::text() 选取当前节点的所有文本子节点,等价于 './text()'
    >>> root.xpath('child::text()')
    ['
        ', '
        ', '
    ']
    >>> root.xpath('./text()')
    ['
        ', '
        ', '
    ']
    
    # child::node() 选取当前节点所有子节点,等价于 './node()'
    >>> root.xpath('child::node()')
    ['
        ', <Element book at 0x2d88878>, '
        ', <Element book at 0x2d88738>, '
    ']
    >>> root.xpath('./node()')
    ['
        ', <Element book at 0x2d88878>, '
        ', <Element book at 0x2d88738>, '
    ']
    
    # descendant::book 选取当前节点所有 book 后代,等价于 './/book'
    >>> root.xpath('descendant::book')
    [<Element book at 0x2d88878>, <Element book at 0x2d88738>]
    >>> root.xpath('.//book')
    [<Element book at 0x2d88878>, <Element book at 0x2d88738>]
    
    # ancestor::book 选取当前节点所有 book 先辈
    >>> root.xpath('.//title')[0].xpath('ancestor::book')
    [<Element book at 0x2d88878>]
    
    # ancestor-or-self::book 选取当前节点的所有 book 先辈以及如果当前节点是 book 的话也要选取
    >>> root.xpath('.//title')[0].xpath('ancestor-or-self::book')
    [<Element book at 0x2d88878>]
    >>> root.xpath('.//book')[0].xpath('ancestor-or-self::book')
    [<Element book at 0x2d88878>]
    >>> root.xpath('.//book')[0].xpath('ancestor::book')
    []
    
    # child::*/child::price 选取当前节点的所有 price 孙节点,等价于 './*/price'
    >>> root.xpath('child::*/child::price')
    [<Element price at 0x2d88878>, <Element price at 0x2d88738>]
    >>> root.xpath('./*/price')
    [<Element price at 0x2d88878>, <Element price at 0x2d88738>]
  • 相关阅读:
    jdk8中奖Date转换为String格式的方法
    Java Calendar详解
    Java Calendar使用总结
    得到当前时间的小时数
    Java中 break continue return 的区别
    两个QWidget叠加,可部分代替layout的功能
    Qt5官方demo解析集35——Music Player(使用winextras模块)
    P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址)——充分证明@是取变量(对象)的地址,而不是变量里面的内容,够清楚!
    提高Android和iOS调试编译速度
    Delphi 快速获取文件大小(使用_lopen和FileSeek,此函数可以快速获取文件大小,即使文件已经被其它程序锁定)
  • 原文地址:https://www.cnblogs.com/ifantastic/p/3863808.html
Copyright © 2020-2023  润新知