How to check if a parameter is defined in XSLT?

Discussion in 'XML' started by Brandolon Hill, Jan 21, 2005.

  1. 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
    Brandolon Hill, Jan 21, 2005
    #1
    1. Advertising

  2. 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
    David Carlisle, Jan 22, 2005
    #2
    1. Advertising

  3. > 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
    Brandolon Hill, Jan 23, 2005
    #3
  4. "Brandolon Hill" <> wrote in message
    news:ct16nt$ruv$...
    >> 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.
    Dimitre Novatchev, Jan 24, 2005
    #4
  5. Brandolon Hill <> 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
    David Carlisle, Jan 24, 2005
    #5
  6. > 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
    Brandolon Hill, Jan 24, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  2. Angel Tsankov
    Replies:
    1
    Views:
    863
    Victor Bazarov
    Apr 5, 2006
  3. Replies:
    1
    Views:
    492
    Sion Arrowsmith
    Jul 10, 2008
  4. Keith Thompson

    Re: __STDC_IEC_559__ (defined or !defined ?)

    Keith Thompson, Aug 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    440
    Keith Thompson
    Aug 17, 2010
  5. Alok
    Replies:
    3
    Views:
    246
Loading...

Share This Page