开发者

XPath - test if at least one node has given value

开发者 https://www.devze.com 2023-04-11 10:36 出处:网络
Given the following XML: <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号