|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

June 10th, 2009, 04:39 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Location: , OH, USA.
Posts: 33
Thanks: 5
Thanked 1 Time in 1 Post
|
|
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!
|

June 10th, 2009, 04:46 PM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
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
|

June 10th, 2009, 05:04 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Location: , OH, USA.
Posts: 33
Thanks: 5
Thanked 1 Time in 1 Post
|
|
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!
|

June 10th, 2009, 05:09 PM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
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:
|
|

June 10th, 2009, 05:19 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
|
|
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 -/
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |