【问题标题】:The XPath id() functionXPath id() 函数
【发布时间】:2014-02-13 05:30:45
【问题描述】:

我在 XPath 中找不到对 id() 函数的任何好的解释。它有什么作用?你是怎么用的?

从可用的描述中,听起来它会给你一个节点的id

所以我就这样玩了,但是报错了:

//bookstore/id(book)

后来有点发烧友尝试了

//bookstore/book[name = id(book/@category)]

没有返回错误,但没有命中。

谁能为我以及未来的 Stack 粉丝和 Google 员工解释一下这个功能?

这是我正在使用的 XML:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J. K. Rowling</author>
  <price>29.99</price>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

<book category="OPENSOURCE">
  <title lang="en">Open Source</title>
  <year>2003</year>
  <price>39.95</price>
</book>

<book category="WEB">
  <title lang="en">WEB</title>
  <year>2012</year>
  <price>21.99</price>
</book>

</bookstore>

【问题讨论】:

  • 您应该使用包含id 属性的文档,不是吗? :)

标签: xml function xpath


【解决方案1】:

如果您在 DTD 中定义了 unique IDs,则 based on the spec 似乎只能使用 id() 函数。

我还遇到了这个SO question,它讨论了 DTD 中的 ID 类型以及如何定义它。

【讨论】:

    【解决方案2】:

    我找不到任何好的解释

    让我推荐我的书 ;-)。

    我认为 id() 函数对您没有帮助,因为我认为您的源文档没有任何 ID 属性。它的属性很少,而且它的属性不是唯一的,所以它们不能是 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多