By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,650 Members | 1,996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,650 IT Pros & Developers. It's quick & easy.

How to check if a parameter is defined in XSLT?

P: n/a
Hi,

Is there a way to check and see if a parameter is defined in XSLT?

I have many stylesheets that all have an <xsl:include> of the same
stylesheet, "foo.xsl".

Foo relies on several parameters that all contain the names of various
XML tags. Foo.xsl then performs a standard action (outputting the
information in HTML) on the tag given by the parameter.

I'm doing this in the context of a content management system. Each
content item has it's own XML structure, so I might have
person.xsl:

car.xsl:
<car>
<model>Gremlin</model>
</car>

<person>
<name>Bob</name>
</person>

Each content type (car, person) has its own XSL, but foo.xsl performs
tasks that need to be done for every content type (generally determining
what locale to display in).

So car.xsl has <xsl:param name="printMeNode" select="string('model')"/>
and person.xsl has <xsl:param name="printMeNode"
select="string('name')"/>. Both car.xsl and person.xsl then do an
xsl:include of foo.xsl.

Then foo.xsl has

xmlns:dynamic="http://exslt.org/dynamic"
....
<xsl:for-each select="dynamic:evaluate($printMeNode)">
<xsl:value-of select="."/>
</xsl:for-each>

(Evaluate is a function that turns a string into a node-set.)

But I'm not the only person who is going to be writing the content type
XSLs, and while I've put dire warnings everywhere I could, I still want
to be able to print an output message if the printMeNode parameter isn't
found so the other developer will know what the heck is going on.

Right now, if printMeNode isn't there, the error message is a cryptic
"Could not find variable with the name of printMeNode". I'm using
Xalan, by the way.

So is there any way to check and ensure that a parameter/variable is
defined? Maybe I'm going about this in the wrong way entirely?

*Any* suggestions, comments will be greatly appreciated.
-------------------
Regards,
BH

--
brandolon (AT) gmail.mybrain.com - take out my brain to reach me

Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

Just define the parameter in the stylesheet wher eit is used with some
dummy value, you can then test if it still has that value and issue an
xsl:message warning.

David
Jul 20 '05 #2

P: n/a
> Just define the parameter in the stylesheet wher eit is used with some
dummy value, you can then test if it still has that value and issue an
xsl:message warning.

David


Well, I'm not worried so much about getting bad value as I am worried
about other developers not knowing about (or understanding) the
parameters that this master stylesheet I'm writing requires. And if
they don't know about the parameter, they won't include it in their
sheets, so I won't have a dummy value to check against.

The entire system is absurdly complex, and I want to make life as easy
as possible for those who will follow me. So if I can just print out a
message saying, "Hey, you forgot to define this parameter. Here's what
it is and here's why you need it," rather than having Xalan issue some
esoteric error message.

Thanks for the suggestion though. I'd definitely keep it in mind for
other situations.
----------------
Regards,
BH

--
brandolon (AT) gmail.mybrain.com - take out my brain to reach me

Jul 20 '05 #3

P: n/a

"Brandolon Hill" <br*******@gmail.mybrain.com> wrote in message
news:ct**********@stan.redhat.com...
Just define the parameter in the stylesheet wher eit is used with some
dummy value, you can then test if it still has that value and issue an
xsl:message warning.

David


Well, I'm not worried so much about getting bad value as I am worried
about other developers not knowing about (or understanding) the parameters
that this master stylesheet I'm writing requires. And if they don't know
about the parameter, they won't include it in their sheets, so I won't
have a dummy value to check against.

The entire system is absurdly complex, and I want to make life as easy as
possible for those who will follow me. So if I can just print out a
message saying, "Hey, you forgot to define this parameter. Here's what it
is and here's why you need it," rather than having Xalan issue some
esoteric error message.

Thanks for the suggestion though. I'd definitely keep it in mind for
other situations.

Savid's suggestion is perfectly usable in your case.

Cheers,
Dimitre Novatchev.
Jul 20 '05 #4

P: n/a
Brandolon Hill <br*******@gmail.mybrain.com> writes:
Just define the parameter in the stylesheet wher eit is used with some
dummy value, you can then test if it still has that value and issue an
xsl:message warning.

David


Well, I'm not worried so much about getting bad value as I am worried
about other developers not knowing about (or understanding) the
parameters that this master stylesheet I'm writing requires. And if
they don't know about the parameter, they won't include it in their
sheets, so I won't have a dummy value to check against.

The entire system is absurdly complex, and I want to make life as easy
as possible for those who will follow me. So if I can just print out a
message saying, "Hey, you forgot to define this parameter. Here's what
it is and here's why you need it," rather than having Xalan issue some
esoteric error message.

Thanks for the suggestion though. I'd definitely keep it in mind for
other situations.
----------------
Regards,
BH

--
brandolon (AT) gmail.mybrain.com - take out my brain to reach me


You misunderstood my suggestion.

there is no reason for you ever to reference an undefined varoable in
xslt. In any stylesheet that uses $foo you can put
<xsl:param name="foo" select="'qwertyuiop'"/>
at the top level of the file so it is always defined.

If you expect that this parameter to be defined on the external call to
the processor, or in an importing stylesheet then your styesheet that is
using $foo can do

<xsl:if test="$foo='qwertyuiop'">
<xsl:message>
You forgot to set the parameter $foo
It should have value 42 unless ....
</xsl:message>
</xsl:if

David

Jul 20 '05 #5

P: n/a
> You misunderstood my suggestion.

Ah, yes I did. I must not have eaten my Wheaties yesterday. Thanks
again for your help! It is most appreciated.
------------------
Regards,
BH

--
brandolon (AT) gmail.mybrain.com - take out my brain to reach me

Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.