473,811 Members | 3,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2041


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="{$starCoun t} stars"
src="starsImage {$starCount}.gi f" />
</xsl:template>

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

That is simply an example assuming that an HTML
<img alt="4 stars" src="starsImage 4.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

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

Similar topics

0
2714
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 extension responsible for the xslt functions. The problem i have is that the obtained transformation is not the waited one. I try to proccess the same XML file with XSL file with a program called XMLspy and i obtained the desire and waited...
4
2125
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 provider who says we need to use XSLT and Web Services to pull the content from their site. Can someone give me a nutshell definition on how this works??? We use Cold Fusion MX on our web server, but I'm having trouble finding a
7
4130
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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" xmlns:my-javascript-ext="my-ext1" extension-element-prefixes="my-javascript-ext"
6
4694
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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" /> <xsl:template match="/">
1
2421
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 stylesheets, that generates .NET assemblies) 2. Performance improvements in the XslCompiledTransform
3
3324
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 (and thank goodness for the new grouping functions), but where are the functions which would really have made it useful without taxing the implementers? e.g trigonometry functions for SVG; square root /log function to do simple statistics? hex/ dec...
12
11606
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 don't want to have to use some external program or script to parse the csv first if possible
2
22781
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 in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself. The most common usage of this is in creating hyperlinks.
1
3464
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 when I output the file to a text file, all the data is wrapping and at the end of each block I am getting the text part of the header included but not all of it. The text that appears is: <br xmlns:msxsl="urn:schemas-microsoft-com:xslt"...
11
4571
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 url="myotherpage.php?name4=value4&foo=bar3&foo2=bar5&name2=value8" /> and so on. Let's suppose I want to retrieve this @url parameter, BUT ONLY with the values, in querystring, associated with "foo" and "foo2" (thus discarding name1, name2, name4 and every other different ones).
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10384
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10395
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4338
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.