• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

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
0
j_young_80
Asked:
j_young_80
  • 2
1 Solution
 
_yossa_Commented:
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>
0
 
metalmickeyCommented:
<xsl:if test="string-length(normalize-space(.)) != 0">
     <br />
</xsl:if>

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

MM
0
 
j_young_80Author Commented:
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>
0
 
_yossa_Commented:

You have to create "if" before building headers, and build them only if there are data for building the second table row.

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



<xsl:template match="root" mode="table">

<xsl:if test="     -- here we test if there are data for builing table content --  ">

<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:if>

</xsl:template>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now