问题描述:

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>

网友答案:

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>
网友答案:
<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>
相关阅读:
Top