Skip to main content
Participant
November 10, 2012
Answered

Displaying a message when there are no results from an XSL Query

  • November 10, 2012
  • 1 reply
  • 1199 views

Using MM_XSLTransform.class.php

I am querying an XML file to produce a list of places whose town name or address match a letter of the alphabet. The letter is passed as a variable in a URL.

XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:param name="letter"/>

<xsl:output method="html" encoding="UTF-8"/>

<xsl:template match="/">

<xsl:for-each select="dataroot/CPINFOWEB[Town=starts-with(Town, $letter) or Name=starts-with(Name, $letter)]">

      <table width="100%" border="0" cellspacing="0" cellpadding="0">

        <tr>

        <td>

    <a href="detail.php?park={CostCode}"><xsl:value-of select="Name"/></a><br />

          <a href="detail.php?park={CostCode}"><xsl:value-of select="Town"/></a><br />

      <xsl:value-of select="Address"/><br />

      <br />    

      </td>

        </tr>

      </table>

      </xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Destination PHP file:

<?php

$mm_xsl = new MM_XSLTransform();

$mm_xsl->setXML("../regions/CPINFOWEB-mobile.xml");

$mm_xsl->setXSL("list.xsl");

$mm_xsl->addParameter("letter",$_GET['letter']);

echo $mm_xsl->Transform();

?>

The result Name and Town are displayed as a URL to link to a "more details" page.

All works fine, but when there are no results for a letter of the alphabet I obviously get a blank page.

How do I display a "no results" message instead

This topic has been closed for replies.
Correct answer David_Powers

You're right, it's counting the total number of records. It's ages since I did anything with XSLT, but I dug out a book that has been gathering dust on my shelf, and came up with the answer. The conditional statement at the bottom should look like this:

<xsl:if test="count(dataroot/CPINFOWEB[Town = starts-with(Town, $letter) or Name = starts-with(Name, $letter)]) = 0">

    <tr>

        <td>No records found</td>

    </tr>

</xsl:if>

I grabbed the XML file from your site, and tested it. This should do the trick.

1 reply

David_Powers
Inspiring
November 10, 2012

I deleted my original reply because the formatting of the XSL code was messed up by the forum. Here's trying again:

Your current code creates a new table for each result. The opening and closing <table> tags should be outside the the <for-each> loop. To deal with the possibility of no results being found, use a conditional statement and the count() function like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:param name="letter"/>

<xsl:output method="html" encoding="UTF-8"/>

<xsl:template match="/">

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<xsl:for-each select="dataroot/CPINFOWEB[Town=starts-with(Town, $letter) or Name=starts-with(Name, $letter)]">

        <tr>

        <td>

    <a href="detail.php?park={CostCode}"><xsl:value-of select="Name"/></a><br />

          <a href="detail.php?park={CostCode}"><xsl:value-of select="Town"/></a><br />

      <xsl:value-of select="Address"/><br />

      <br />    

      </td>

        </tr>

</xsl:for-each>

<xsl:if test="count(dataroot/CPINFOWEB/Town) &lt; 1">

    <tr>

    <td>No records found</td>

    </tr>

    </xsl:if>

</table>

</xsl:template>

</xsl:stylesheet>

Participant
November 10, 2012

Thanks for the reply David.

It's not quite working - is it because the if test is counting the root data, rather than checking the result of the select query?

If Z is the inital letter query, there will be no results, but there are still Towns in the original data.

I added the second original query to the if test:

<xsl:if test="count(dataroot/CPINFOWEB/Town) and count(dataroot/CPINFOWEB/Name) &lt; 1">

But still no result. To check the syntax, I changed less than to greater than, and the message appeared.

This is the mobile site: http://britannia-parking.co.uk/mobile/find-a-car-park.html

Thanks again.

David_Powers
David_PowersCorrect answer
Inspiring
November 11, 2012

You're right, it's counting the total number of records. It's ages since I did anything with XSLT, but I dug out a book that has been gathering dust on my shelf, and came up with the answer. The conditional statement at the bottom should look like this:

<xsl:if test="count(dataroot/CPINFOWEB[Town = starts-with(Town, $letter) or Name = starts-with(Name, $letter)]) = 0">

    <tr>

        <td>No records found</td>

    </tr>

</xsl:if>

I grabbed the XML file from your site, and tested it. This should do the trick.