如何使用 Xpath 选择具有多个类的元素? [英] How can I select an element with multiple classes with Xpath?

查看:32
本文介绍了如何使用 Xpath 选择具有多个类的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上面的 xml 示例中,我想使用 xpath 选择属于 foo 类而不是 bar 类的所有书籍.

In the above xml sample I would like to select all the books that belong to class foo and not in class bar by using xpath.

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
  <book class="foo">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book class="foo bar">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book class="foo bar">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

推荐答案

通过用前导和尾随空格填充 @class 值,您可以测试foo"和bar"的存在不用担心它是第一个、中间的还是最后一个,以及对食物"或贫瘠"@class 值的任何误报:

By padding the @class value with leading and trailing spaces, you can test for the presence of " foo " and " bar " and not worry about whether it was first, middle, or last, and any false positive hits on "food" or "barren" @class values:

/bookstore/book[contains(concat(' ',@class,' '),' foo ')
        and not(contains(concat(' ',@class,' '),' bar '))]

这篇关于如何使用 Xpath 选择具有多个类的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆