?
Solved

Handling Empty XML recordsets via XSLT

Posted on 2004-10-25
4
Medium Priority
?
474 Views
Last Modified: 2008-03-04
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
Comment
Question by:j_young_80
  • 2
4 Comments
 
LVL 1

Expert Comment

by:_yossa_
ID: 12408782
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
 
LVL 6

Expert Comment

by:metalmickey
ID: 12409013
<xsl:if test="string-length(normalize-space(.)) != 0">
     <br />
</xsl:if>

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

MM
0
 

Author Comment

by:j_young_80
ID: 12416118
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
 
LVL 1

Accepted Solution

by:
_yossa_ earned 200 total points
ID: 12418985

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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question