Louis Maddox
Louis Maddox

Reputation: 5566

How to write an xpath for related attribute

I'm looking to obtain the surname of the author as designated in the following XML tree:

...
<contrib contrib-type="author">
          <name>
            <surname>Obama</surname>
            <given-names>Barack</given-names>
          </name>
          <xref rid="aff1" ref-type="aff"/>
          <xref ref-type="corresp" rid="cor1">*</xref>
        </contrib>

I don't know if this is possible but can I define the Xpath

//xref[@ref-type="corresp"]

and then get the surname value?

/name/surname

I've never written an xpath value before and in the W3schools tutorial / playing around with a generator I can only work out how to query sub-levels, e.g. getting contrib-type="author" then selecting the associated surname(s). Here what I want is on 'the same level' I think, i.e. below <contrib>

Upvotes: 1

Views: 366

Answers (1)

acdcjunior
acdcjunior

Reputation: 135762

To get the surname starting from the xref[@ref-type="corresp"] node, you can navigate to their parent using .. (will take you to the related contrib) and then go down again up to the surname using name/surname:

//xref[@ref-type="corresp"]/../name/surname

Upvotes: 2

Related Questions