Scala 中的简单 Xpath 查询 [英] Simple Xpath query in scala

查看:41
本文介绍了Scala 中的简单 Xpath 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Scala 运行 XPath 查询,但它似乎不起作用.我的 Xml 看起来像(简化):

I'm trying to run a XPath query with scala and it doesn't seem to work. My Xml looks like ( simplified):

<application>
  <process type="output" size ="23"> 
     <channel offset="0"/>
      ....
     <channel offset="4"/>
  </process>
  <process type="input" size ="16"> 
     <channel offset="20"/>
      ....
     <channel offset="24"/>
  </process>
</application>

我想用 input 属性检索 process,为此我使用这个 XPath 查询:

I want to retrieve the process with the input attribute and for that i use this XPath query:

//process[@type='input']

这应该有效,我用 xpathtester 验证了它现在,我的 Scala 代码如下所示:

This should work, i verified it with xpathtester Now, my scala code looks like:

import scala.xml._
val x = XML.loadFile("file.xml")

val process = (x \\ "process[@type='input']")  // will return empty NodeSeq() !!!

process 最终为空,它没有捕获我想要的.我是这样解决的:

The process ends up empty, it does't capture what I want. I worked it around like this:

val process = (x \\ "process" filter( _ \"@type" contains Text("input")))

这更丑陋.为什么我的原始查询不起作用的任何已知原因?

which is much uglier. Any known reason why my original query shouldn't work?

推荐答案

XPath"不应用于描述 Scala 标准库支持的内容.XPath 是一种成熟的表达式语言,目前有两个最终版本,第三个正在开发中:

"XPath" should not be used to describe what the Scala standard library supports. XPath is a full-fledged expression language, with so far two final versions and a third in the works:

  • XPath 1.0 from 1999
  • XPath 2.0 from 2007 (2nd edition 2010)
  • XPath 3.0 from 2013 (candidate recommendation)

充其量你可以说 Scala 有一个非常小的 XPath 启发的操作子集.因此,您不能指望采用 XPath 表达式并将它们直接粘贴到 Scala 中而不做更多工作.

At best you could say that Scala has a very small subset of XPath-inspired operations. So you can't expect to take XPath expressions and directly paste them to Scala without doing a bit more work.

第三方库可以更好地支持实际的 XPath 表达式,包括:

Third-party libraries can give you better support for actual XPath expressions, including:

  • 缩放 Xml
    • Scala 库
    • 提供比普通 Scala XML 更像 XPath 的体验,路径看起来像 XPath,工作起来也很像(具有许多相同的功能和轴)"
    • 如果我理解得很好,它仍然不是真正的 XPath
    • 旨在与 Scala 很好地集成
    • Java 库
    • 开源
    • 对 XPath 2(和 XSLT 2)的完整且一致的支持
    • 有一个适用于 DOM 和其他数据模型的 XPath API,但目前没有特定的 Scala 支持

    这篇关于Scala 中的简单 Xpath 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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