Count non empty nodes in XSL

tia_kamakshi
tia_kamakshi used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

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

Author

Commented:
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

Author

Commented:

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

Thanks again
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Information Architect
Top Expert 2006
Commented:
It would be more something like this

<xsl:template match="/">
    <xsl:value-of select="count(.//CCC[normalize-space(.)])"/>
</xsl:template>

it counts the CCC nodes that have text content taht is not empty or whites-apce only

Commented:
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>

Author

Commented:
Many Thanks for your help
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial