XSLT Functions

Discussion in 'XML' started by daz_oldham, May 24, 2006.

  1. daz_oldham

    daz_oldham Guest

    Hi

    I am doing an XSLT that processes (for example) a list of hotels.
    Each hotel has the attribute @StarRating which is one of the following
    values:

    *
    **
    ***
    ****
    *****

    Is it possible to pass my attribute to a custom function that could
    then do in effect a case statement on them, replacing it with an image
    to represent the star rating?

    I am sure that it is, but if not, how do you work around this?

    Many thanks

    Darren
     
    daz_oldham, May 24, 2006
    #1
    1. Advertising

  2. daz_oldham wrote:


    > Each hotel has the attribute @StarRating which is one of the following
    > values:
    >
    > *
    > **
    > ***
    > ****
    > *****
    >
    > Is it possible to pass my attribute to a custom function that could
    > then do in effect a case statement on them, replacing it with an image
    > to represent the star rating?


    Are you using XSLT 1.0 or 2.0?
    Only XSLT 2.0 allows you to write custom functions with XSLT itself.
    With XSLT 1.0 you need to write a named template e.g.

    <xsl:template name="get-image">
    <xsl:param name="stars" />
    <xsl:variable name="starCount" select="string-length($stars)" />
    <img alt="{$starCount} stars"
    src="starsImage{$starCount}.gif" />
    </xsl:template>

    You can then call that template with e.g.
    <xsl:call-template name="get-image">
    <xsl:with-param name="stars" select="@StarRating" />
    </xsl:call-template>

    That is simply an example assuming that an HTML
    <img alt="4 stars" src="starsImage4.gif">
    should be generated for the attribute value '****'.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 24, 2006
    #2
    1. Advertising

  3. daz_oldham

    daz_oldham Guest

    I am using XSLT 1.0 Martin, changing my version to 2.0 in my xslt
    didn't seem to make any difference, so I am presuming that with what I
    am doing I could fairly easily switch to version 2 and use the
    functions.

    Or am I being a little naiive there? ;)

    Many thanks

    Daz
     
    daz_oldham, May 24, 2006
    #3
  4. daz_oldham

    Andy Dingley Guest

    daz_oldham wrote:

    > I could fairly easily switch to version 2 and use the functions.


    Calling a named template under XSLT is perhaps a little verbose,
    compared to other languages, but it's a simple and straightforward
    solution that's easily coded and understood by any XSLT coder. I'd
    stick with 1.0
     
    Andy Dingley, May 24, 2006
    #4
  5. daz_oldham wrote:

    > I am using XSLT 1.0 Martin, changing my version to 2.0 in my xslt
    > didn't seem to make any difference, so I am presuming that with what I
    > am doing I could fairly easily switch to version 2 and use the
    > functions.


    I am not sure what you want to achieve and doing actually, you can use
    an XSLT stylesheet with version="1.0" and run that with an XSLT 2.0
    processor, you can use a stylesheet with version="2.0" and run that with
    an XSLT 1.0 processor, those scenarios are possible but each has its
    problems and pecularities. Usually it is best to use an XSLT 1.0
    processor to run XSLT 1.0 stylesheet and an XSLT 2.0 processor to run
    XSLT 2.0 stylesheets.

    The main difference between an XSLT 2.0 function and an XSLT 1.0 named
    template is that you can call a function from any XPath expression while
    you always need the xsl:call-template instruction to call a named template.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 24, 2006
    #5
  6. daz_oldham

    daz_oldham Guest

    Well Martin, Andy - you've both lost me - not that it is something that
    is difficult to do ;)

    I will continue in 1.0 - I am working in ASP.NET 2.0 for the web, so I
    will have no problems there.

    As I say, I am looking to do basic lookups on things like "*****" for
    hotel star ratings and things like that - nothing too difficult.

    Thanks for your help - I really appreciate it as I say to everyone who
    pitches in on the newsgroups - its a good community :)

    Daz
     
    daz_oldham, May 24, 2006
    #6
  7. Personally, I'd use the string-length function to get the number of
    asterisks, and use that to construct the name of the image. Depending on
    how much control you have over the source of the documents, you might
    first want to apply an appropriate translate() operation to discard any
    whitespace characters.

    If you need something more sophisticated, it can probably be done...
     
    Joe Kesselman, May 25, 2006
    #7
  8. daz_oldham

    daz_oldham Guest

    There is a little bit more to it than this, in that the star ratings
    are not actually consistent.

    They may come through as 2STARS, 2* and other variations. If I were
    doing this in C#, I'd just do a case statement on the string and then
    replace it with an image name, and ideally this is what I would have
    liked to have done in my XSLT.

    Regards

    Darren
     
    daz_oldham, May 25, 2006
    #8
  9. daz_oldham

    Andy Dingley Guest

    On 25 May 2006 01:25:24 -0700, "daz_oldham" <>
    wrote:

    >They may come through as 2STARS, 2* and other variations.


    Do you have to deal with "two" and variants? Hopefully not.

    What you could do is to take the input and translate it twice. Once to
    extract the stars (discard other characters) and take the length, the
    other to extract the digits (discard other characters) and evaluate it
    as a number. Use the number value if it's >0, otherwise take the
    star-count.
     
    Andy Dingley, May 25, 2006
    #9
  10. daz_oldham wrote:
    > They may come through as 2STARS, 2* and other variations. If I were
    > doing this in C#, I'd just do a case statement on the string and then
    > replace it with an image name, and ideally this is what I would have
    > liked to have done in my XSLT.


    If that's what you want, the suggestion elsewhere of a named template
    may indeed be the best one. In XSLT 1.0, named templates can't be called
    as functions... but their results can be assigned to variables so you
    can do most of the same things with them, albeit a bit more verbosely.



    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, May 25, 2006
    #10
  11. daz_oldham

    daz_oldham Guest

    I think this may be the way to go for me on this one Joe.

    I am starting to wonder if indeed XSLT is the way to go on this, and
    maybe I should parse the XML with XMLDOM in my ASPx pages.

    Many thanks for your input.

    Darren

    Joe Kesselman wrote:
    > daz_oldham wrote:
    > > They may come through as 2STARS, 2* and other variations. If I were
    > > doing this in C#, I'd just do a case statement on the string and then
    > > replace it with an image name, and ideally this is what I would have
    > > liked to have done in my XSLT.

    >
    > If that's what you want, the suggestion elsewhere of a named template
    > may indeed be the best one. In XSLT 1.0, named templates can't be called
    > as functions... but their results can be assigned to variables so you
    > can do most of the same things with them, albeit a bit more verbosely.
    >
    >
    >
    > --
    > () ASCII Ribbon Campaign | Joe Kesselman
    > /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    daz_oldham, Jun 2, 2006
    #11
    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. Stylus Studio
    Replies:
    0
    Views:
    689
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,691
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    625
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    725
    Henry S. Thompson
    Oct 19, 2005
  5. Xiangliang Meng
    Replies:
    1
    Views:
    1,613
    Victor Bazarov
    Jun 21, 2004
Loading...

Share This Page