yiwei
yiwei

Reputation: 4180

Get attribute of parent using xpath query

I have an XML file that is like this:

<PurchaseOrders>
    <PurchaseOrder PoNum="5000" OrderDate="2006-02-18" Status="Unshipped">
        <item>
            <partid>100-100-01</partid>
            <name>Snow Shovel, Basic 22 inch</name>
            <quantity>3</quantity>
            <price>9.99</price>
        </item>
    </PurchaseOrder>
<PurchaseOrders>
... some more Purchase Orders

And I would like to return the PurchaseOrder's PoNum which contains an item with partid = "100-100-01". So far I have an XPath query that looks like:

/PurchaseOrders/PurchaseOrder[item/partid="100-100-01"]

But this only returns the PurchaseOrder. How can I get the PoNum attribute of this? That is, I want to obtain the value of PoNum, which in this case is "5000".

Upvotes: 0

Views: 211

Answers (1)

Justin Ko
Justin Ko

Reputation: 46826

Use:

/PurchaseOrders/PurchaseOrder[item/partid="100-100-01"]/@PoNum

The @PoNum returns the attribute. Basically, use @ to select attributes.

Upvotes: 1

Related Questions