How to select a node's first child name? XPath

XpathChildren

Xpath Problem Overview


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?

Xpath Solutions


Solution 1 - Xpath

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])

Solution 2 - Xpath

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])

Solution 3 - Xpath

You need something like:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJaqView Question on Stackoverflow
Solution 1 - Xpathuser357812View Answer on Stackoverflow
Solution 2 - XpathRyan BergerView Answer on Stackoverflow
Solution 3 - XpathmwittrockView Answer on Stackoverflow