参考资料

  1. xpath定位方法有哪些
  2. XPath 选取若干路径
  3. XPath 谓语(Predicates)
  4. XPath 轴(Axes)
  5. xpath定位
  6. 正则表达式语法速查
  7. XPath 在 XSLT 中使用
  8. 正则生成代码有哪些

XPath 路径表达式

XPath 路径表达式

  1. 基本概念

  • XML路径语言

  • 用于在XML文档中导航和选择节点

  • W3C标准

  1. 节点类型

  • 元素节点

  • 属性节点

  • 文本节点

  • 命名空间节点

  • 处理指令节点

  • 注释节点

  • 文档(根)节点

  1. 路径表达式语法

  • 绝对路径:/root/child

  • 相对路径:child/grandchild

  • 当前节点:.

  • 父节点:..

  • 通配符:*

  1. 常用表达式

  • 选择所有节点://*

  • 选择特定元素://element

  • 选择带有属性的元素://element[@attribute]

  • 选择特定属性值://element[@attribute='value']

  1. 谓语(Predicates)

  • 用于查找特定节点

  • 示例://book[1] (第一个book元素)

  • 示例://book[price>35] (价格大于35的book)

  1. 轴(Axes)

  • ancestor

  • ancestor-or-self

  • attribute

  • child

  • descendant

  • descendant-or-self

  • following

  • following-sibling

  • namespace

  • parent

  • preceding

  • preceding-sibling

  • self

  1. 运算符

  • | (联合)

    • (加)

    • (减)

    • (乘)

  • div (除)

  • = (等于)

  • != (不等于)

  • < (小于)

  • <= (小于等于)

  • (大于)

  • = (大于等于)

  • or

  • and

  • mod (取模)

  1. 函数

  • 节点函数:position(), last(), count()

  • 字符串函数:concat(), substring(), contains()

  • 布尔函数:true(), false(), not()

  • 数字函数:sum(), floor(), ceiling()

  1. 示例

  • 选择所有book元素://book

  • 选择title属性为"XML"的book://book[@title='XML']

  • 选择price大于30的book://book[price>30]

  • 选择第一个book的子title:/bookstore/book[1]/title

  • 选择所有title和price元素://title | //price