简体   繁体   English

选择一个其兄弟姐妹包含属性XML的元素

[英]Selecting an element whose sibling contains an attribute XML

I'm trying to select the title value where the attribute value is greater than 1: 我试图选择属性值大于1的标题值:

path="//book/price[@value >1]

I'm not exactly sure how to select only the titles where the attribute value for the element price is greater than one. 我不确定如何仅选择元素价格的属性值大于1的标题。 I'm an XML beginner and I'm doing a few exercises from a tutorial. 我是XML初学者,并且正在从教程中进行一些练习。

My priorities are 我的重点是

  1. learning XML 学习XML
  2. super XML 超级XML

XML: XML:

<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <price value="1">49.99</price>
</book>
<book>
 <title lang="en">Learning XML</title>
 <price value="2">29.95</price>
</book>
<book>
  <title lang="en">super xml</title>
  <price value="3">39.95</price>
</book>
</bookstore>

Update 更新资料

The first query was perfect could you show me how to obtain it from a nested element? 第一个查询是完美的,您能告诉我如何从嵌套元素中获取它吗?

<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <possibleprice>
     <price value="1">49.99</price>
  </possibleprice>
</book>
<book>
  <title lang="en">Learning XML</title>
  <possibleprice>
     <price value="2">29.95</price>
  </possibleprice>
</book>
<book>
  <title lang="en">super xml</title>
  <possibleprice>
    <price value="3">39.95</price>
  </possibleprice>
</book>
</bookstore>

For example: 例如:

For the first version of the OP: 对于OP的第一个版本:

Input: 输入:

<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <price value="1">49.99</price>
</book>
<book>
 <title lang="en">Learning XML</title>
 <price value="2">29.95</price>
</book>
<book>
  <title lang="en">super xml</title>
  <price value="3">39.95</price>
</book>
</bookstore>

XPath: XPath:

//book/price[@value > 1]/preceding-sibling::title

Result: 结果:

<title lang="en">Learning XML</title>
<title lang="en">super xml</title>

For the updated second XML version of OP, as different example: 对于OP的更新的第二个XML版本,例如:

XPath: XPath:

//book/possibleprice/price[@value > 1]/ancestor::book/title

Same result as above. 与上述相同的结果。

To answer the question in the comment if it would be necessary to use preceding-sibling twice for the 2nd version: no. 要回答评论中的问题,是否有必要对第二个版本使用两次同级兄弟:否。 preceding-sibling targets the preceding-sibling of the current node. preceding-sibling以当前节点的preceding-sibling为目标。 The current node in this case is price and has no siblings, but the parent possibleprice . 在这种情况下,当前节点是price ,没有兄弟姐妹,但parent 可能price To get the same result in a different way as given above, it would be possible to get the parent of price and then the preceding-sibling of possibleprice : 为了获得上述给出一个不同的方式相同的结果,将有可能得到parent价格 ,然后将preceding-sibling possibleprice的:

//book/possibleprice/price[@value > 1]/parent::possibleprice/preceding-sibling::title

which results in the same. 结果相同。

As mentioned that OP is a beginner in XML, maybe the following can be useful to illustrate XPath axes: http://www.xmlplease.com/axis 如前所述,OP是XML的初学者,也许以下内容对说明XPath轴很有用: http : //www.xmlplease.com/axis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM