How to use a template and variable name to count some values of a specific node in xsl -


i use template as

 <xsl:template name="mytemplate">  

and need count amount of level nodes values "on" , "off".

the final report want have:

this file contains 3 "on" values , 2 "off" values. 

look @ part of xml file.

<?xml version="1.0" encoding="iso-8859-1"?> <?xml:stylesheet type='text/xsl' href='view.xsl'?> <doc> <show>view<show/> <entry>  <light>ae</light>  <level>on</level> </entry> <entry>  <light>by</light>  <level>off</level> </entry> <entry>  <light>ac</light>  <level>off</level> </entry> <entry>  <light>pc</light>  <level>on</level> </entry> <entry>  <light>tc</light>  <level>on</level> </entry> 

thank help

these simple xpaths trick:

count(/*/*/level[. = 'on']) 

and

count(/*/*/level[. = 'off']) 

for verification, when xslt:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">   <xsl:output omit-xml-declaration="yes" indent="yes" method="text"/>   <xsl:strip-space elements="*"/>    <xsl:template match="/*">     <xsl:text>the number of on nodes is: </xsl:text>     <xsl:value-of select="count(/*/*/level[. = 'on'])"/>     <xsl:text/>     <xsl:text>the number of off nodes is: </xsl:text>     <xsl:value-of select="count(/*/*/level[. = 'off'])"/>   </xsl:template>  </xsl:stylesheet> 

...is applied against provided xml:

<doc>   <show>view</show>   <entry>     <light>ae</light>     <level>on</level>   </entry>   <entry>     <light>by</light>     <level>off</level>   </entry>   <entry>     <light>ac</light>     <level>off</level>   </entry>   <entry>     <light>pc</light>     <level>on</level>   </entry>   <entry>     <light>nc</light>     <level>on</level>   </entry> </doc> 

...the wanted result produced:

the number of on nodes is: 3 number of off nodes is: 2 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -