54

I have an XML from which I have to select the name of the child of one of the nodes. I'm kind of a beginner in this, so I didn't find the Xpath expression to do it. I know the level of the node

Example

Name from /Employee/Department/

but Department has children nodes of unknown names. I have to select the first child of Department node. How can I do this?

1
  • Could someone add the 'child' tag? I had trouble finding this question and it helped me a lot. Adding it requires 1500 repuation hence I can't do it.
    – Happy Bird
    Apr 26, 2018 at 13:32

3 Answers 3

73

You wrote:

I have to select the first child of Department node

You could use:

/Employee/Department/*[1]

Then, you also wrote:

I have an XML from which I have to select the name of the child of one of the nodes

So, you could use:

name(/Employee/Department/*[1])
1
  • 9
    +1. This is assuming that the OP wants the first element child, which is probably a valid assumption.
    – LarsH
    Aug 30, 2010 at 16:16
24

I don't know the exact context of your XML, but I believe this is the XPath you are looking for...

/Employee/Department/*[1]

The key part of this XPath is *[1], which will select the node value of the first child of Department.

If you need the name of the node, then you will want to use this...

name(/Employee/Department/*[1])
10

You need something like:

local-name(/Employee/Department/*[1])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.