Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test for zero records when using <xsl:for-each>?

Tags:

xslt

My code generates the following XML:

<person_app>
  <person_data>
    <person>
         ...person details here...
    </person>
  </person_data>
</person_app>

Using XSLT, I parse the person records as follows:

<xsl:template match="/person_app/person_data">
  <xsl:for-each select="person">
      ...generate person HTML...
  </xsl:for-each>
</xsl:template>

However, in cases when I receive zero people, I'd like to display "No records found" (or something similar). When the app returns zero records, the XML resembles the following:

<person_app/>

Long story short, how can I test for an empty result set when I use <xsl:for-each/> to parse my Person records? I've tried the following with no success:

<xsl:if test="not(person)">
  <div style="font-size:18pt"><xsl:text>No records found</xsl:text></div>
</xsl:if>
like image 590
Huuuze Avatar asked Mar 04 '09 21:03

Huuuze


1 Answers

Something like this:

<xsl:choose>
  <xsl:when test="person">
    <xsl:for-each select="person">
       ...generate person HTML...
    </xsl:for-each>
  </xsl:when>
  <xsl:otherwise>
    <div style="font-size:18pt"><xsl:text>No records found</xsl:text></div>
  </xsl:otherwise>
</xsl:choose>
like image 126
andynormancx Avatar answered Oct 01 '22 13:10

andynormancx