Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all child nodes except text nodes?

Tags:

xml

xpath

Given XML that looks something like this

 <FirstChild Orientation="Vertical">
    <SecondChild>Some text</SecondChild>
 </FirstChild>

and a binding that is using

XPath="child::node()"

I get output that looks like this

enter image description here

I don't want the text to appear in the tree

I tried using not like

XPath="child::node()[not(text())]"

as well as many variations, but haven't been able to nail it.

Anyone know how select all nodes except text nodes?

BTW, I do not want to exclude comments, only text.

like image 244
Forty-Two Avatar asked Sep 25 '13 20:09

Forty-Two


People also ask

What is child :: In XPath?

The child axis indicates the children of the context node. If an XPath expression does not specify an axis, the child axis is understood by default. Since only the root node or element nodes have children, any other use will select nothing.

What is childNodes in javascript?

Child nodes include elements, text and comments. Note: The NodeList being live means that its content is changed each time new children are added or removed. The items in the collection of nodes are objects, not strings. To get data from node objects, use their properties.

Which property of a node represents the content of an HTML node?

The textContent property of the Node interface represents the text content of the node and its descendants. Note: textContent and HTMLElement.


1 Answers

Your XPath expression excludes all child nodes which contain a text node.

child::node()[not(text())]

Exclude nodes that are a text node themselves:

child::node()[not(self::text())]
like image 124
Jens Erat Avatar answered Oct 04 '22 14:10

Jens Erat