XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Todd EischeidSubject: displaying specific html when the result of a query has count = 0
Author: Todd Eischeid
Date: 30 Sep 2007 10:43 AM
Hi All,

I have a simply xml file that is basically a glossary, and I supply a term to it and I have an XQuery string that does a lookup on the term. I'm using an xsl style sheet to format the results for the browser window.

I thought it would be simple (and it may be) to print a single 'term not found' type of message to the screen if the term was not in the xml glossary file. If the term *is* in the xml file, the code works ok, but I cannot get the desired result when the term is not in the file. Seems like this should be simple, but I'm just stumped. Any help is greatly appreciated.

Below is my xsl file, I am supplying the term via setting the parameter 'paramVal' to the desired term:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="paramVal"/>

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="/glossary/definition[@name=$paramVal]" mode="found"></xsl:apply-templates>
<xsl:apply-templates select="/glossary/definition" mode="not-found"></xsl:apply-templates>
</body>
</html>
</xsl:template>

<xsl:template match="/glossary/definition[@name=$paramVal]" mode="found">

<h3 style="margin-top:0em;margin-left:0px;"><xsl:value-of select="@name"/></h3>
<p><xsl:value-of select="."/></p>

<p><xsl:value-of select="last()"/></p>

</xsl:template>


<xsl:template match="/glossary/definition" mode="not-found">

<xsl:if test="count(/glossary/definition[@name=$paramVal])&lt;1">

<h3 style="margin-top:0em;margin-left:0px;"><xsl:value-of select="@name"/> </h3>

<p style="color:gray;">This term could not be located in the glossary.</p>
</xsl:if>

</xsl:template>

</xsl:stylesheet>

Postnext
James DurningSubject: displaying specific html when the result of a query has count = 0
Author: James Durning
Date: 02 Oct 2007 11:02 AM
The problem is that if you use apply-templates, you'll be applying the templates once for each definition. Not only is it slow, but you'll get the results over and over.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="paramVal"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="/glossary/definition[@name=$paramVal]"/>
<xsl:if test="not(/glossary/definition[@name=$paramVal])">
<h3 style="margin-top:0em;margin-left:0px;">
<xsl:value-of select="@name"/>
</h3>
<p style="color:gray;">This term could not be located in the glossary.</p>
</xsl:if>
</body>
</html>
</xsl:template>
<xsl:template match="/glossary/definition[@name=$paramVal]">
<h3 style="margin-top:0em;margin-left:0px;">
<xsl:value-of select="@name"/>
</h3>
<p><xsl:value-of select="."/></p>
<p><xsl:value-of select="last()"/></p>
</xsl:template>
</xsl:stylesheet>

Posttop
Todd EischeidSubject: displaying specific html when the result of a query has count = 0
Author: Todd Eischeid
Date: 02 Oct 2007 08:42 PM
Thank you so much for the help. Your revised xsl works great!

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.