Solved

Handling Empty XML recordsets via XSLT

Posted on 2004-10-25
419 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
Question by:j_young_80
    4 Comments
     
    LVL 1

    Expert Comment

    by:_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>
    0
     
    LVL 6

    Expert Comment

    by:metalmickey
    <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
    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:

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now