Read endpoints in XML using XSL

I want to be able to read the "endpoints" in an XML file using XSL. By endpoint, I mean a tag without children that contain data.

i.e.

<xmlsnippet> 
    <tag1>NOTENOUGHDAYS</tag1> 
    <tag2>INVALIDINPUTS</tag2> 
    <tag3> 
        <tag4> 
            <tag5>2</tag5> 
            <tag6>1</tag6> 
        </tag4> 
    </tag3> 
</xmlsnippet> 

This XML should return 4 since there are 4 "endpoints"

+3
source share
3 answers
<xsl:template match="/>
  <xsl:value-of select="count(//*[not(*) and normalize-space() != ''])" />
</xsl:template>

This repeats the entire XML tree through the descendant axis ( //), scans all element nodes ( *) that have no children ( not(*)) and contain data other than spaces ( normalize-space() != '').

The resulting node -set result is counted (and returns 4 in your case).

+6
source

*[not(*)] , .

edit: count(elements)

+3

Try: -

 <xsl:variable name="numOfLeafNodes" select="count(//*[not(*)])" />

, xml. : -

 <xsl:variable name="numOfLeafNodes" select="count(.//*[not(*)])" />

to find the number of leaf nodes that are descendants of the current node context.

+2
source

Source: https://habr.com/ru/post/1739276/


All Articles