0

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

1 Answer 1

1

Use:

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

So I'm using this XML Developer extension with Eclipse and it keeps telling me that this command returns an empty sequence...
It works for me, using the same Eclipse extension and your sample document above (correcting the closing tag to </PurchaseOrders>). Can you elucidate? Show sample XML and XPath which don't work?
@YiweiGao, is PurchaseOrders actually the root node of the xml document? The question suggests that there may actually be multiple PurchaseOrders - ie there are multiple roots or there is a different root node. You could try starting the xpath with // instead of /.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.