Question

Locked

XPath: Select multiple child nodes

By philrunninger ·
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,

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

I found the answer

by philrunninger In reply to XPath: Select multiple ch ...

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

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

Collapse -

That will get a bit expensive

by Tony Hopkinson In reply to I found the answer

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.

Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums