tags:

views:

413

answers:

3

I have a variable in my xsl that may or may not be declared. How can I tell if it has been declared.

The issue is I have 2 style sheets. A parent (where the global variable is declared) and a child (where the global variable is referenced). I usually call the parent stylesheet which then calls the child stylesheet. So everything is fine because the variable is declared. But I sometimes call the child style sheet by itself.

So the easiest solution for me is a way to check if the variable has been declared before trying to use it.

Any help is greatly appreciated.

A: 

This is not necessary.

XSLT is a declarative language. You can't use a stylesheet that has undeclared references, because that is a compile-time error, not a run-time error.

Which means that if the stylesheet compiles at all, it will never run into an "undeclared variable" error by definition.

What are you trying to do?

Tomalak
See my changes. (I have multiple stylesheets.) Thanks for the input though!
joe
A: 

Assuming your stylesheet is referencing a second sheet where you are not certain of the contents, try xsl:import (as opposed to xsl:include).

steamer25
The child stylesheet never imports or includes the parent. I just need to tell at runtime if the variable has been initialized.
joe
What mechanism are you using to establish the relationship if not import or include? If you import, you can declare the variable in such a way that import precedence will either set a value to the variable or set an empty string. You can then test whether $foo!=''.
steamer25
The parent is importing the child, but that is not really relevant. Because the problem is that sometimes I have ANT calling only the child stylesheet, not the parent. The variable is never declared in the child so this throws an error. I need to be able to check if the variable is declared, and perform some action.
joe
So in your child, declare <xsl:variable name="foo" select="''"/>. This will be overridden in the parent when the child is imported. You can then test in the child <xsl:choose><xsl:when test="$foo=''">Child only</xsl:when><xsl:otherwise>Called via Parent</xsl:otherwise></xsl:choose>. The variable will always be present but you can initialize it only when you call from the parent.
steamer25
A: 

In xsl you cannot tell if a variable is declared.

The solution I came up with, was having an interim xsl file that declared the variable and then imported the "child" xsl file.

joe