如何使用XPath选择多个可能的文本值?

Jar*_*red 7 xml xpath

我必须从类似下面的评级标签中选择评级代码,但仅限于代理商为"SP"或"SNP"时.现在我有:

./ratings/rating/agency[text()='SNP'|text()='SP']/../code
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用.我究竟做错了什么?

<ratings>
  <rating>
    <agency>SP</agency>
    <provider>SP</provider>
    <type>LONG TERM</type>
    <currencyType>LOCAL</currencyType>
    <description>SP Standard LT LC rating</description>
    <code>BBB+</code>
    <date>2011-09-07</date>
  </rating>
</ratings>
Run Code Online (Sandbox Code Playgroud)

谢谢,

贾里德

Lar*_*rsH 13

主要的是|你试图用作'或' 的union运算符.将其更改为or:

./ratings/rating/agency[text()='SNP' or text()='SP']/../code
Run Code Online (Sandbox Code Playgroud)

或者更自然地,

ratings/rating[agency[. = 'SNP' or . = 'SP']]/code
Run Code Online (Sandbox Code Playgroud)

在XPath 2.0中,您可以使用序列:

ratings/rating[agency = ('SNP', 'SP')]/code
Run Code Online (Sandbox Code Playgroud)