xml - XPath id() 函数

标签 xml function xpath

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

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

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

//bookstore/id(book)

然后变得有点狂热并尝试

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

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

有人可以为我、 future 的 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>

最佳答案

看起来 based on the spec如果您定义了 unique IDs,您将只使用 id() 函数在你的 DTD 中。

The id function selects elements by their unique ID.

...

An element node may have a unique identifier (ID). This is the value of the attribute that is declared in the DTD as type ID.

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

关于xml - XPath id() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21733583/

相关文章:

c++ - C++中 vector 的叉积

c - 在 Objective-C 中前向声明具有已定义数组大小的函数

python - 使用带有 Selenium 的 XPath 打开新选项卡并从该选项卡中获取内容时出错

xpath - 我正在尝试获取一个值的 Xpath 但嵌套条件出错

java - Android StackScrollLayout

android - 绘制到 SurfaceView 时出现应用程序错误

javascript - 将函数视为值时是否会忽略 DOM 命令?

html - 以本地语言显示内容 : R

java - 具有不同 xml 名称的相同响应对象

java - 无效的 XPath 表达式 : expecting token