Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xpath

children

People also ask

What is child :: In XPath?

As defined in the W3 XPath 1.0 Spec, " child::node() selects all the children of the context node, whatever their node type." This means that any element, text-node, comment-node and processing-instruction node children are selected by this node-test.

How do I select the last child in XPath?

"last() method" selects the last element (of mentioned type) out of all input element present.

How do you get to the nth sub element using the XPath?

By adding square brackets with index. By using position () method in 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])

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

You need something like:

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