How to check if a parameter is defined in XSLT?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brandolon Hill

    How to check if a parameter is defined in XSLT?

    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>Gremli n</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="printMeNo de" select="string( 'model')"/>
    and person.xsl has <xsl:param name="printMeNo de"
    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($prin tMeNode)">
    <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.c om - take out my brain to reach me

  • David Carlisle

    #2
    Re: How to check if a parameter is defined in XSLT?


    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

    Comment

    • Brandolon Hill

      #3
      Re: How to check if a parameter is defined in XSLT?

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

      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.c om - take out my brain to reach me

      Comment

      • Dimitre Novatchev

        #4
        Re: How to check if a parameter is defined in XSLT?


        "Brandolon Hill" <brandolon@gmai l.mybrain.com> wrote in message
        news:ct16nt$ruv $1@stan.redhat. com...[color=blue][color=green]
        >> 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[/color]
        >
        > 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.[/color]


        Savid's suggestion is perfectly usable in your case.

        Cheers,
        Dimitre Novatchev.


        Comment

        • David Carlisle

          #5
          Re: How to check if a parameter is defined in XSLT?

          Brandolon Hill <brandolon@gmai l.mybrain.com> writes:
          [color=blue][color=green]
          > > 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[/color]
          >
          > 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.c om - take out my brain to reach me[/color]

          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="'qwerty uiop'"/>
          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='qwe rtyuiop'">
          <xsl:message>
          You forgot to set the parameter $foo
          It should have value 42 unless ....
          </xsl:message>
          </xsl:if

          David

          Comment

          • Brandolon Hill

            #6
            Re: How to check if a parameter is defined in XSLT?

            > 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.c om - take out my brain to reach me

            Comment

            Working...