tags:

views:

238

answers:

2

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>
+4  A: 

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>
andynormancx
No dice. That didn't work either.
Huuuze
Strange, it should have.
andynormancx
I agree. I even broke things down into very simple pieces and still nothing.
Huuuze
Problem found and fixed. I had forgotten to close the xsl:otherwise tag. Definitely works now.
andynormancx
Got the answer. It failed the template match at the top.
Huuuze
+1  A: 
<xsl:template match="/person_app/person_data">
  <xsl:if test="count(person) = 0">
    <div style="font-size:18pt"><xsl:text>No records found</xsl:text></div>
  </xsl:if>
  <xsl:for-each select="person">
      ...generate person HTML...
  </xsl:for-each>
</xsl:template>
Nils Weinander