在上面的xml示例中,我想通过使用xpath选择所有属于类foo但不在类栏中的书.
<?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>
Run Code Online (Sandbox Code Playgroud)
Mad*_*sen 36
通过@class使用前导和尾随空格填充值,您可以测试"foo"和"bar"的存在,而不用担心它是第一个,中间还是最后一个,以及对"食物"或"贫瘠"的任何误报" @class价值观:
/bookstore/book[contains(concat(' ',@class,' '),' foo ')
and not(contains(concat(' ',@class,' '),' bar '))]
Run Code Online (Sandbox Code Playgroud)
Den*_*fel 11
虽然我喜欢Mads解决方案:这是XPath 2.0的另一种方法:
/bookstore/book[
tokenize(@class," ")="foo"
and not(tokenize(@class," ")="bar")
]
Run Code Online (Sandbox Code Playgroud)
请注意以下表达式都是正确的:
("foo","bar")="foo" -> true
("foo","bar")="bar" -> true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18004 次 |
| 最近记录: |