Question Locked

XPath: Select multiple child nodes

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Given the following sample "xml" file (tags won't display correctly, so I used spaces instead of angle brackets. All nodes are elements, no attributes)...

USGovernment
--President
----Order 1 /Order
----FirstName George /FirstName
----LastName Washington /LastName
--/President
--... and so on
/USGovernment

I can easily select the President nodes of George and Honest Abe.

//President[Order='1' or Order='16']

But how would I return their names without repeating the query string above?

//President[Order='1' or Order='16']/FirstName | //President[Order='1' or Order='16']/LastName

works, but is there a shorthand way of writing this? In my real data that repeated portion of the query is much longer.

Thanks,

3 replies

by Member 27801784

Here's an answer I found, after continuing to research the problem.

//President[Order='1' or Order='16']/*[name()='FirstName' or name()='LastName']

by tony hopkinson

when you have a lot of nodes in president, you are returining all of them and then serially testing each one with name.

//President[Order='1' or Order='16']//FirstName | //LastName

I think, and order is a standout candidate for being an attribute.

following-sibling axis could also prove useful, but unless you have schema that enforces element ordering , a bit fragile.

All firstnames or lastnames with a parent of President with a child of ORDER with a value or 1 or 16 is another way to look at it.

This discussion is closed to new replies.