XPath - test if at least one node has given value
Given the following XML:
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>
I would l开发者_如何学运维ike to check if at least one node has year of for example '1982'. Something like:
<xsl:if test="........" ><p>passed!</p></xsl:if>
In both XPath 1.0 and Xpath 2.0:
'1982' = /catalog/cd/year
Variations:
XPath 1.0:
1982 = /catalog/cd/year
XPath 2.0:
1982 = /catalog/cd/year/number()
The XPath =
operator is very powerful when at least one of its arguments is a node-set (or a sequence in XPath 2.0). The result is true if and only if the (atomized)value of at least one of the nodes in the node-set (sequence) is equal to the other argument of =
. This is exactly what this question is asking for.
Try test="index-of(/catalog/cd/year, 1982)".
How about:
/catalog/cd/year = 1982
This comparison (called general comparison) operator returns true if at least one item on the left side "matches" one item on the right side.
精彩评论