Groovy的GPath表达式的完整语法是什么?

Dan*_*vak 11 groovy

在尝试解析Groovy中的RSS提要时,我发现了一个使用通配符的GPath示例:

def text = """ 
<data> 
   <common-tables> 
    <table name="address"/> 
    <table name="phone"/> 
  </common-tables> 

  <special-tables> 
    <table name="person"/> 
  </special-tables> 

  <other-tables> 
    <table name="business"/> 
  </other-tables> 
</data> 
""" 

def xml = new XmlParser().parse(new ByteArrayInputStream(text.getBytes())) 
def tables = xml.'**'.table.findAll{ it.parent().name() == 
"special-tables" || it.parent().name
Run Code Online (Sandbox Code Playgroud)

(来自http://old.nabble.com/Q:-Avoiding-XPath---using-GPath-td19087210.html)

它似乎是对'spread-dot'操作符的有趣用法.我在Groovy网站,书籍等上找不到任何参考.

这是如何工作的,更重要的是,你是如何发现这一点的?那里有GPath'Rosetta Stone'的XPath吗?

giz*_*zmo 20

好吧,像往常一样,查找信息的最佳位置是Groovy源代码本身.
解析的结果是groovy.util.slurpersupport.GPathResult对象.

如果查看源代码(普通java文件),您会看到getProperty(string)方法具有以下特殊运算符:

  • ".."返回父级
  • "*"返回所有孩子
  • "**"充当深度第一循环
  • "@"用于访问属性
  • 普通节点访问器.

这就是目前没有其他神奇的关键词.


noa*_*oah 5

所有这些字符串都被视为属性。他们都不是真正的运营商。

调用通过GPathResult#getProperty路由,它专门检查 Gizmo 的答案中列出的运算符。