p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password
Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other programmers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 10th, 2009, 04:39 PM
Authorized User
Points: 137, Level: 2
Points: 137, Level: 2 Points: 137, Level: 2 Points: 137, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , OH, USA.
Posts: 33
Thanks: 5
Thanked 1 Time in 1 Post
Send a message via ICQ to iceandrews Send a message via AIM to iceandrews 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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old June 10th, 2009, 04:46 PM
mhkay's Avatar
Wrox Author
Points: 13,389, Level: 50
Points: 13,389, Level: 50 Points: 13,389, Level: 50 Points: 13,389, Level: 50
Activity: 82%
Activity: 82% Activity: 82% Activity: 82%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,025
Thanks: 0
Thanked 114 Times in 112 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old June 10th, 2009, 05:04 PM
Authorized User
Points: 137, Level: 2
Points: 137, Level: 2 Points: 137, Level: 2 Points: 137, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , OH, USA.
Posts: 33
Thanks: 5
Thanked 1 Time in 1 Post
Send a message via ICQ to iceandrews Send a message via AIM to iceandrews 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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old June 10th, 2009, 05:09 PM
mhkay's Avatar
Wrox Author
Points: 13,389, Level: 50
Points: 13,389, Level: 50 Points: 13,389, Level: 50 Points: 13,389, Level: 50
Activity: 82%
Activity: 82% Activity: 82% Activity: 82%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,025
Thanks: 0
Thanked 114 Times in 112 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
iceandrews (June 10th, 2009)
  #5 (permalink)  
Old June 10th, 2009, 05:19 PM
samjudson's Avatar
Friend of Wrox
Points: 4,955, Level: 29
Points: 4,955, Level: 29 Points: 4,955, Level: 29 Points: 4,955, Level: 29
Activity: 82%
Activity: 82% Activity: 82% Activity: 82%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,453
Thanks: 0
Thanked 50 Times in 50 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 -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 04: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
use vbscript declared variable in asp georgerascle ASP Pro Code Clinic 3 June 5th, 2003 07:33 PM



All times are GMT -4. The time now is 12:36 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
© 2010 Wiley Publishing, Inc