获取子节点包含属性的节点

2024-01-10

假设我有以下 XML:

<book category="CLASSICS">
  <title lang="it">Purgatorio</title>
  <author>Dante Alighieri</author>
  <year>1308</year>
  <price>30.00</price>
</book>

<book category="CLASSICS">
  <title lang="it">Inferno</title>
  <author>Dante Alighieri</author>
  <year>1308</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <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>

我想做一个 xpath 来获取所有具有语言属性为“it”的标题节点的书籍节点。

我的尝试看起来像这样:

//book[title[@lang='it']]

但这没有用。我希望找回节点:

<book category="CLASSICS">
  <title lang="it">Purgatorio</title>
  <author>Dante Alighieri</author>
  <year>1308</year>
  <price>30.00</price>
</book>

<book category="CLASSICS">
  <title lang="it">Inferno</title>
  <author>Dante Alighieri</author>
  <year>1308</year>
  <price>30.00</price>
</book>

有什么提示吗?


Try

//book[title/@lang = 'it']

内容如下:

  • get all book elements
    • that have at least one title
      • which has an attribute lang
        • 值为"it"

你可能会发现this http://www.rpbourret.com/xml/XPathIn5.htm有帮助——这是一篇题为“XPath 五段” http://www.rpbourret.com/xml/XPathIn5.htm作者:罗纳德·伯雷。

但老实说,//book[title[@lang='it']]除非您的 XPath 引擎有“问题”,否则上面的内容应该是等效的。因此,它可能是您未向我们展示的代码或示例 XML 中的某些内容 - 例如,您的示例是 XML 片段。难道根元素有一个命名空间,而您在查询中没有考虑到它?你只告诉我们这不起作用,但你没有告诉我们你得到了什么结果。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取子节点包含属性的节点 的相关文章

随机推荐