2011-10-28 95 views
5

我必須從類似於下面的評級標籤中選擇評級代碼,但僅限代理機構爲'SP'或'SNP'時。現在我有:如何使用XPath選擇多個可能的文本值?

./ratings/rating/agency[text()='SNP'|text()='SP']/../code 

雖然這似乎沒有工作。我究竟做錯了什麼?

<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> 

感謝,

賈裏德

回答

9

最主要的是工會操作|你已經試過作爲 '或' 使用。將其更改爲or

./ratings/rating/agency[text()='SNP' or text()='SP']/../code 

或者更自然,

ratings/rating[agency[. = 'SNP' or . = 'SP']]/code 

在XPath 2.0,你可以使用一個序列:

ratings/rating[agency = ('SNP', 'SP')]/code 
1

使用or而不是|

./ratings/rating/agency[text()='SNP' or text()='SP']/../code