Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 10th, 2009, 03:39 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default check if a variable has been declared.

Is there a method that allows you to check to see if a variable/parameter has been declared before calling it? Obviously calling an undeclared variable/parameter results in an error.

Logically speaking I'm looking for something along these lines.

Code:
 
<xsl:choose>
     <xsl:when test="$Test"/>
     <xsl:otherwise/>
</xsl:choose>
A parallel function that I have in mind is the "doc-available ()" function. Using this I can continue processing even if the document URI doesn't resolve. I'd like a similar functionality but with a variable. (Or perhaps even a <xsl:call-template/>. Check if there is a named template that exists with that name before calling it.)

I'm using XSLT 2.0 & Saxon-9SA

Thank you much!
 
Old June 10th, 2009, 03:46 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, there's no such method. (Does any other language have such a thing? Perhaps some purely interpreted shell languages.) Whether or not a variable is declared is a compile-time condition entirely under the stylesheet author's control, which is a big difference from doc-available() which tests a run-time condition outside the stylesheet's control. (And different from function-available(), which tests a compile-time condition outside the programmer's control).
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 10th, 2009, 04:04 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

That's what I figured.

The issue arose when using the <saxon:call-template name="{$tname}"/> instruction. We're dynamically creating a few templates on an external source, out of our control.

But not ALL the named templates are constructed dynamically. Occasionally there's an error when a template is called with no matching named pair. So it would've been a simple way to check for existence inside the stylesheet itself before deciding which templates to call. (Apart from examining the input documents)

Just to be clear, you can't use "element-available()" to check for a NAMED called template instruction rather than the generic one.

Thanks!
 
Old June 10th, 2009, 04:09 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You might be better off creating match-templates dynamically rather than named templates. You can create them to match elements in some namespace that doesn't exist in your source, and then invoke them by apply-templates to some dummy element created for the purpose. There's then a clean fallback if no template rule matches.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
iceandrews (June 10th, 2009)
 
Old June 10th, 2009, 04:19 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You could load the external template using document() and then check if it contains the named template manually of course?

<xsl:if test="document('template.xsl')//xsl:template[@name=$Test]">
...
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to check variable value gantait XSLT 3 February 20th, 2007 12:17 AM
Check two string values on a single variable?? ramesh.kumarm XSLT 2 June 29th, 2006 03:29 AM
best way to check for variable being empty crmpicco Classic ASP Basics 3 March 28th, 2006 12:11 PM
Check for existence of a session variable apd8x ASP.NET 1.0 and 1.1 Basics 2 November 12th, 2003 11:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.