XSLT - count the number of child elements by using XPath XSLT - count the number of child elements by using XPath xml xml

XSLT - count the number of child elements by using XPath


Your first xsl:when test is incorrect here

<xsl:when test="actor[count(*) > 1]/name">

You are already positioned on an actor element here, so this will be looking for an actor element that is a child of the current actor element, and finding nothing.

You probably just want to do this

<xsl:when test="count(*) > 1">

Alternatively, you could do this

<xsl:when test="*[2]">

i.e, is there an element in the second position (that saves counting all elements, when you only really want to check there is more than one),

Or perhaps you want to check the current actor element has an element other than name?

<xsl:when test="*[not(self::name)]">

As an aside, it might be better to put the test in a template match, rather than use an xsl:choose.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="no"/>   <xsl:template match="/">      <div>         <xsl:apply-templates select="database/movies/movie"/>      </div>   </xsl:template>   <xsl:template match="movie">      <xsl:value-of select="concat('Title: ', title)"/>      <br/>      <xsl:text>Actors: </xsl:text>      <xsl:apply-templates select="actors/actor"/>      <br/>   </xsl:template>   <xsl:template match="actor">      <xsl:value-of select="name"/>      <br/>   </xsl:template>   <xsl:template match="actor[*[2]]">      <a href="actor_details.php?actorID={@actorID}">         <xsl:value-of select="name"/>      </a>      <br/>   </xsl:template></xsl:stylesheet>

Note that the XSLT processor should match the more specific template first in this instance.