Link to home
Start Free TrialLog in
Avatar of j_young_80
j_young_80

asked on

Handling Empty XML recordsets via XSLT

I currently have an XSLT that build a table and fills it with data based on a stored procedure running for xml auto. This all works perfectly if the stored proc returns data but if an empty recordset is returned I just get the static table data returned to the user.

Is there a way that I can test in the XSLT if the XML data is empty and then return something else like <br>No Data</br> if the table content returns nothing. below is what I currently have:

<xsl:template match="/">
      <xsl:apply-templates select="*" mode="table"/>
</xsl:template>

<xsl:template match="root" mode="table">
<div style="overflow:scroll;height=91%">
<table>                  
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th><b><font color="green">Header 4</font></b></th>
<th><b><font color="green">Header 5</font></b></th>
<th><b><font color="green">Header 6</font></b></th>
<th><b><font color="blue">Header 7</font></b></th>
<th><b><font color="blue">Header 8</font></b></th>
<th><b><font color="blue">Header 9</font></b></th>
</tr>
<xsl:apply-templates select="*" mode="tablecontent"/>                                          
</table>
</div>
</xsl:template>

is there a test I can do on an apply-templates to check whether it has returned any data?
thanks,
j
Avatar of _yossa_
_yossa_

No. But you can test before apply templates:

   <root>
       <table/>
        <table>
              <row/>
        </table>
   </root>

template for root:

 <xsl:choose>
      <xsl:when test="table">
//Here we have a table
           <xsl:apply-templates select="table"/>
      </xsl:when>
      <xsl:otherwise>
//No tables found          
       </xsl:otherwise>
 </xsl:choose>
<xsl:if test="string-length(normalize-space(.)) != 0">
     <br />
</xsl:if>

this will test if you have a node with no content.

MM
Avatar of j_young_80

ASKER

Yossa,

i'm not exactly sure how your example works - what is the line <table/> representing?

Also with my above example, a static row of headers are built, and then a second row containing the content is attempted to be applied, is there any way in the initial call that I can make sure that the SECOND table row has been built, otherwise display nothing??

<xsl:template match="/">
     <xsl:apply-templates select="*" mode="table"/>
</xsl:template>
ASKER CERTIFIED SOLUTION
Avatar of _yossa_
_yossa_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial