Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Count non empty nodes in XSL

Hi,

Using XSL how do we count non empty nodes

Like in below xml example If we have to count non empty CCC nodes

value should come 2

Please help me in xsl code

<AAA>
  <BBB>
    <CCC id="c1">CCC1</CCC>
    <CCC id="c2">CCC2</CCC>
    <CCC id="c20"/>
    <CCC/>
  </BBB>
</AAA>


Many Thanks
Avatar of kmartin7
kmartin7
Flag of United States of America image

<xsl:template match="*">
<xsl:value-of select="count(text())"/>
</xsl:template>
Avatar of tia_kamakshi

ASKER

Thanks for your quick response

I have modified my XML as below but value is not coming 3 this time

<?xml version="1.0" encoding="utf-8" ?>
<AAA>
  <BBB>
    <CCC id="c1">CCC1</CCC>
    <CCC id="c2">CCC2</CCC>
    <CCC id="c20">CCC3</CCC>
    <CCC/>
  </BBB>
</AAA>

Please help me fixing xsl

Thanks again

Also, I need count of nodes <CCC> which is non blank

Thanks again
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry - I was in a rush to a meeting.

I think Geert is on the right track. I assumed you wanted to count ALL nodes that contained text, not just certain elements. To accomplish this, try the following:

<xsl:template match="*">
   <xsl:text>Text nodes: </xsl:text><xsl:value-of select="count(//text()[normalize-space()])"/>
</xsl:template>
Many Thanks for your help