首页 > 解决方案 > 通过 XPath 根据条件获取 XML 值

问题描述

下面是我的 XML:

<bookstore>
   <book>
      <title>Python</title>
      <price>29.99</price>
   </book>
   <book>
      <title>XML</title>
      <price>29.99</price>
      <feature>
           <description>Learn XML</description>
      </feature>
   </book>
</bookstore>

如果description "Learn XML"匹配,那么它应该返回title "XML".

标签: xmlxpath

解决方案


试试这个 XPath

//description[text() = 'Learn XML']/../../title

它匹配所有内容为“Learn XML”的元素并获取description其.text()title


推荐阅读