How to check if a parameter is defined in XSLT?

B

Brandolon Hill

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.
 
D

David Carlisle

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
 
B

Brandolon Hill

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.
 
D

Dimitre Novatchev

Brandolon Hill said:
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.
 
D

David Carlisle

Brandolon Hill said:
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.

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
 
B

Brandolon Hill

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top