Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Handling Empty XML recordsets via XSLT

Posted on 2004-10-25
4
Medium Priority
?
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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 …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

618 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