473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

XSLT Functions

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

May 24 '06 #1
10 1988


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/
May 24 '06 #2
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

May 24 '06 #3

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

May 24 '06 #4


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/
May 24 '06 #5
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

May 24 '06 #6
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...
May 25 '06 #7
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

May 25 '06 #8
On 25 May 2006 01:25:24 -0700, "daz_oldham" <Da**************@gmail.com>
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.
May 25 '06 #9
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
May 25 '06 #10
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


Jun 2 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
4
by: Ringo Langly | last post by:
Hi all, I'm a seasoned web programmer, but I've never touched XSLT. It's always been one of those acronyms I've never needed to educate myself on. Now... we're working with a web content...
7
by: RC | last post by:
First, let me say I couldn't find a group discuss XML/XSLT. So I only choose the closest groups to post this message. Here is part of my *.xsl file <xsl:stylesheet...
6
by: RC | last post by:
Hello World, I am try do call a JavaScript function from XSLT, but I got function not avaible error. See "????" below. Would someone out there tell me how? Thank Q! <xsl:stylesheet...
1
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
3
by: shaun roe | last post by:
mild rant follows Working now for a couple of years with xslt and now xslt 2.0, does anyone else get the impression that xslt 2.0 somehow missed the point? Yes its got a fancy new data model...
12
by: Chris | last post by:
Hi, Just wondering if anyone out there knows if it is possible to convert a CSV to xml using XSLT? I've seen a lot of examples of xml to CSV, but is it possible to go back the other way? I...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
1
by: CAM123 | last post by:
I have added: <br><xsl:value-of select="Line" /></br> to my XSLT stylesheet to get a line per repeating block. When I view the output as XML it looks perfect - one line per block. However...
11
by: Ebenezer | last post by:
Let's suppose I have some nodes in an XML file, with an URL attribute: <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.