Discussion:
XSLT: check existence of a variable
(too old to reply)
Thomas Scheffler
2003-09-17 10:36:39 UTC
Permalink
Hi,

I have several xslt documents that import each other in a specific way
to allow resulting documents to be layout in a common way. In the
"master stylesheet" I need some kind of test to check if a variable exists.
<xsl:if test="$testvariable"> is false when the variable exists. Is
there any test that delivers "true" if the variable exists and return
"false" if not instead of an error message saying that the variable is
not defined? I hope somebody could help me on this issue.

Thank you in advance

Thomas Scheffler
Oleg Tkachenko
2003-09-17 11:49:46 UTC
Permalink
Post by Thomas Scheffler
I have several xslt documents that import each other in a specific way
to allow resulting documents to be layout in a common way. In the
"master stylesheet" I need some kind of test to check if a variable exists.
<xsl:if test="$testvariable"> is false when the variable exists. Is
there any test that delivers "true" if the variable exists and return
"false" if not instead of an error message saying that the variable is
not defined?
It's quite difficult to implement such checking, may be because it's quite
weird task. If you import a stylesheet don't you aware of all variables it
contains? If a variable does not exist, you stylesheet is just invalid and
cannot be used.
Can you explain, why do you need it.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Thomas Scheffler
2003-09-17 18:57:15 UTC
Permalink
Post by Oleg Tkachenko
Post by Thomas Scheffler
I have several xslt documents that import each other in a specific way
to allow resulting documents to be layout in a common way. In the
"master stylesheet" I need some kind of test to check if a variable exists.
<xsl:if test="$testvariable"> is false when the variable exists. Is
there any test that delivers "true" if the variable exists and return
"false" if not instead of an error message saying that the variable is
not defined?
It's quite difficult to implement such checking, may be because it's
quite weird task. If you import a stylesheet don't you aware of all
variables it contains? If a variable does not exist, you stylesheet is
just invalid and cannot be used.
Can you explain, why do you need it.
Of cause it is weird else it would be forseen that this is needed by
someone.
I try to explain how it works. I have a multilanguage xslt presenting
system that consists of quite a few files. Name-structure of the file is
somehow the following:

requestedFeature-requestedLanguage.xsl

this file defines variables with values of native language

requestedFeature-lang.xsl

this is the gereric file imported by the file above and responsible for
the layout specific to the requested feature

Layout-requestedLanguage.xsl

this is as file #1 a file of variables in native language that imports

Laxout.xsl

where all the main layout happens.


Because I have some predefined settings happen in the last file because
elsewhere I would write to much. But in rather seldom cases I need
influence on that. Thatswhy SOME of the toplevel files contain a
variable, to make it easy to maintain it, all the other won't. Now I
need a check for a variable in Layout.xsl if this variable exists. I
coudn't imagin how this could be done, it must be a tricky why like
grouping or such thing....but I hope it's not impossible....

Greets

Thomas
Brian Wilson
2003-09-21 22:00:02 UTC
Permalink
Try parameterizing your stylesheets instead of using variables alone. In the
declaration, give them a known undefined value:

<xsl:param name="new-variable" select="'undefined'"/>

Now you can test against the uninitialized value and know that the variable
will always exist.

You should also look at some other methods of making your templates more
dynamic so that you don't need to use stylesheet management to control the
transformation. Named templates, template modes, parameterized document()
calls, etc. Check out the last couple of chapters of the O'Reilly XSLT
Cookbook, at what he calls "template tags."
Post by Thomas Scheffler
Hi,
I have several xslt documents that import each other in a specific way
to allow resulting documents to be layout in a common way. In the
"master stylesheet" I need some kind of test to check if a variable exists.
<xsl:if test="$testvariable"> is false when the variable exists. Is
there any test that delivers "true" if the variable exists and return
"false" if not instead of an error message saying that the variable is
not defined? I hope somebody could help me on this issue.
Thank you in advance
Thomas Scheffler
Loading...