Using
count(list/language[contains(., 'java')])
Full XSLT conversion :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
java -- <xsl:value-of select=
"count(/*/*/list/language[contains(., 'java')]) "/>
</xsl:template>
</xsl:stylesheet>
when applied to the provided XML document :
<mainNode>
<book>
<price> 100 </price>
<city> chennai </city>
<list>
<language> c java ruby </language>
</list>
</book>
<book>
<price> 200 </price>
<city> banglore </city>
<list>
<language> c java </language>
</list>
</book>
<book>
<price> 300 </price>
<city> delhi </city>
<list>
<language> java ruby </language>
</list>
</book>
</mainNode>
required, the correct result is obtained :
java -- 3
Update
If we count all the occurrences of the string, and not just all the nodes containing the string, here's how to do it:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:param name="pWord" select="' java '"/>
<xsl:template match="/">
<xsl:variable name="vResult">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="concat($pWord, '--- ')"/>
<xsl:value-of select="string-length($vResult)"/>
</xsl:template>
<xsl:template match="list/language" name="countWord">
<xsl:param name="pText" select="."/>
<xsl:if test="contains($pText, $pWord)">
<xsl:text>X</xsl:text>
<xsl:call-template name="countWord">
<xsl:with-param name="pText"
select="concat(' ', substring-after($pText, $pWord))"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
, XML-:
<mainNode>
<book>
<price> 100 </price>
<city> chennai </city>
<list>
<language> c java ruby </language>
</list>
</book>
<book>
<price> 200 </price>
<city> banglore </city>
<list>
<language> c java </language>
</list>
</book>
<book>
<price> 300 </price>
<city> delhi </city>
<list>
<language> java java ruby </language>
</list>
</book>
</mainNode>
, :
java --- 4