zsaren
zsaren

Reputation: 3

Return one attribute with XPath given multiple conditions

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

Upvotes: 0

Views: 300

Answers (1)

dogbane
dogbane

Reputation: 274612

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.

Upvotes: 1

Related Questions