开发者

Return one attribute with XPath given multiple conditions

开发者 https://www.devze.com 2023-04-07 20:04 出处:网络
I\'m trying to retrieve one (and only one) occurrence of the element /Document/docOf/serviceEvent/effectiveTime/@value

I'm trying to retrieve one (and only one) occurrence of the element

/Document/docOf/serviceEvent/effectiveTime/@value 

when the

/Document/docOf/tempId/@root="1.3.5"

The docOf elements can occur in any order, there is no guarantee that the ones with the sought after tempId are the first ones in the xml.

I've been trying to use the position() function in combination with tempId/@root="1.3.5" but find that it is not working the way I intend it to. If I write

tempId/@root="1.3.5" and position()=1 

I get the correct result, but only when the tempId/@root="1.3.5" elements appear before the ones with other tempIds. How do I retrieve the effectiveTime/@value from an element with the 开发者_JAVA技巧correct tempId/@root and retrieve it just once?

<Document>
    <docOf>
        <tempId root="1.3.2"codeSystem="11.2.3"/>
        <serviceEvent>
            <code code="UXZX0A"/>
        </serviceEvent>
    </docOf>
    <docOf>
        <tempId root="1.3.5"/>
        <serviceEvent classCode="ACT">
            <effectiveTime value="20101122145613+0100"/>
        </serviceEvent>
    </docOf>
    <docOf>
        <tempId root="1.3.5"/>
        <serviceEvent classCode="ACT">
            <effectiveTime value="20101122145613+0100"/>
        </serviceEvent>
    </docOf>
    <docOf>
        <tempId root="1.3.2"/>
        <serviceEvent>
            <code code="UXZX0A" codeSystem="11.2.3"/>
        </serviceEvent>
    </docOf>
</Document>


Try:

/Document/docOf[tempId/@root='1.3.5'][1]/serviceEvent/effectiveTime/@value

It first gets all docs with root=1.3.5 and then selects the first node from that set. It then extracts the value attribute from it.

0

精彩评论

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