Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: How to select all nodes that don't have children nodes

Tags:

xml

xslt

xpath

I want to insert the names of all of the nodes and their values into an xml. The problem that I'm having is one of the first child nodes doesn't have any values assigned to it, but it does have child nodes which have values. If I just use a wildcard then it just selects all of the child nodes on that level including the one without a value assigned to it. Is there a way to use wildcard to only select children nodes that don't have children nodes themselves?

like image 323
OstrichProjects Avatar asked Jun 10 '13 19:06

OstrichProjects


People also ask

How do you get all the children of a node?

To get all child nodes of an element, you can use the childNodes property. This property returns a collection of a node's child nodes, as a NodeList object. By default, the nodes in the collection are sorted by their appearance in the source code.

Can NodeList have child nodes?

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.

How do I select all child elements in XPath?

For the div element with an id attribute of hero //div[@id='hero'] , these XPath expression will select elements as follows: //div[@id='hero']/* will select all of its children elements. //div[@id='hero']/img will select all of its children img elements. //div[@id='hero']//* will select all of its descendent elements.

What tag that Cannot have as a child node?

An empty element is an element from HTML, SVG, or MathML that cannot have any child nodes (i.e., nested elements or text nodes).


1 Answers

the predicate [not(node())] is true for all nodes without child nodes but that includes text and comment nodes, perhaps you want [not(*)] which is just true those nodes without element children.

 <xsl:for-each select="//*[not(*)]">
    <xsl:value-of select="concat('&#10;',name(),': ',."/>
 </xsl:for-each>

therefore iterates over all the leaf elements with no element children and prints the element name and content

like image 150
David Carlisle Avatar answered Nov 15 '22 09:11

David Carlisle