xml - Can't write out elements when useing xsl:if and count() together -


i want write out artist elements if total number of artist elements greater x. have following xml:

<catalog> <cd>     <title>empire burlesque</title> </cd> <cd>     <title>hide heart</title>     <artist scale="28">bonnie tyler</artist> </cd> <cd>     <title>greatest hits</title>     <artist scale="30">dolly parton</artist> </cd> <cd>     <title>still got blues</title>     <artist scale="24">gary moore</artist> </cd> 

i have following in xsl:

 <xsl:for-each select="/">   <xsl:if test="count(catalog/cd/artist) &gt; 26">     <tr>       <td><xsl:value-of select="title"/></td>       <td><xsl:value-of select="artist"/></td>     </tr>   </xsl:if> </xsl:for-each> 

what doing wrong?

there several errors in xslt code:

  • using titles instead of title in select-expression
  • you checking counts greater 26, sample xml has 3 entries
  • you selecting root node in xsl:for-each, tried access child nodes 2 levels down xsl:value-of

this stylesheet satisfies requirments:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:template match="/catalog">   <xsl:if test="count(cd) > 2">    <xsl:for-each select="cd">     <tr>       <td><xsl:value-of select="title"/></td>       <td><xsl:value-of select="artist"/></td>     </tr>    </xsl:for-each>   </xsl:if> </xsl:template> </xsl:stylesheet> 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -