473,320 Members | 1,829 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,320 software developers and data experts.

XSL transformation not works for code reuse

Hi friends,

I'm beginner in XSL/XSLT. And i m very impressive, as it is separating
presentation and data layer.

Now my question is related to XSL transform.

Below are sample files one is Books.XML and other is Books.XSL, please
refer it

--------------------------------------------------------

<!-- Books.XML -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Books.XSL"?>
<Books>
<Book id="1">
<Name>Master In C#</Name>
<Rating>3.5</Rating>
</Book>
<Book id="2">
<Name>Professional C#</Name>
<Rating>4.3</Rating>
</Book>
<Book id="3">
<Name>XSLT In 21 Days</Name>
<Rating>3.6</Rating>
</Book>
<Book id="4">
<Name>.NET Professional</Name>
<Rating>4.1</Rating>
</Book>
<Book id="5">
<Name>Professional JAVA</Name>
<Rating>4.0</Rating>
</Book>
<Book id="6">
<Name>Head First Design Patter</Name>
<Rating>4.9</Rating>
</Book>
</Books>
--------------------------------------------------------

<!-- Books.XSL -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>

<xsl:template name="format-book">
<xsl:param name="Rate" />
<xsl:param name="Book" />

<xsl:choose>
<xsl:when test="$Rate &lt; 4">
<tr bgcolor="red">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr bgcolor="lightblue">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:otherwise>
</xsl:choose>

</xsl:template>

<xsl:template match="/">
<html>
<head>My XSLT Testing</head>
<body>
<table>
<tr bgcolor="BBCCAA"><td>Book Name</td><td>Rating</td>
</tr>
<xsl:for-each select="/Books/Book">
<xsl:call-template name="format-book">
<xsl:with-param name="Rate" select="./Rating" />
<xsl:with-param name="Book" select="." />
</xsl:call-template>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

--------------------------------------------------------
Now if any book's rating is less then 4 then will be display in red
othewise it's in blue....
And it's working fine with above code.....
......but hey...look at template node of XSL file.
there is some condition to check rating. Everything is ok, I'm checking
when-otherwise. But in both When and Otherwise tag, i need to implement
same code to display book and rating in a <TR> (table row)...
Where is RE-USE....
This sample is working fine, but think if user want change in
presentation style then we need to update both code, although they are
doing same stuff.....and also think if coder want to change structure
of XML file then also we need to change both parts (ie. When tag and
Otherwise tag).

Is there any solution...
Is it possible that i can check rating and make decision wich <TR>
(color) tag to implement....but the actual view remain same, like....

<xsl:choose>
<xsl:when test="$Rate &lt; 4">
<tr bgcolor="red">
</xsl:when>
<xsl:otherwise>
<tr bgcolor="lightblue">
</xsl:otherwise>
</xsl:choose>

<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>

</tr>

I know above soltion is wrong and aginst the law of XML format.

But is there any alternative??????
looking for ur replies and feedback
Rushikesh

Dec 1 '05 #1
4 1340
"Rushi" <IM***********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
<xsl:template name="format-book">
<xsl:param name="Rate" />
<xsl:param name="Book" />

<xsl:choose>
<xsl:when test="$Rate &lt; 4">
<tr bgcolor="red">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr bgcolor="lightblue">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:otherwise>
</xsl:choose>

</xsl:template> This sample is working fine, but think if user want change in
presentation style then we need to update both code, although they are
doing same stuff.....and also think if coder want to change structure
of XML file then also we need to change both parts (ie. When tag and
Otherwise tag). But is there any alternative??????


Divide and conquer! Create a new named template that outputs the <tr>
element and call that template from <when> and <otherwise>.

// Magnus
Dec 2 '05 #2
"Magnus Henriksson" <ma***************@NOSPAMericsson.com> wrote in message
news:dm**********@news.al.sw.ericsson.se...
"Rushi" <IM***********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
<xsl:template name="format-book">
<xsl:param name="Rate" />
<xsl:param name="Book" />

<xsl:choose>
<xsl:when test="$Rate &lt; 4">
<tr bgcolor="red">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr bgcolor="lightblue">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:otherwise>
</xsl:choose>

</xsl:template>

This sample is working fine, but think if user want change in
presentation style then we need to update both code, although they are
doing same stuff.....and also think if coder want to change structure
of XML file then also we need to change both parts (ie. When tag and
Otherwise tag).

But is there any alternative??????


Divide and conquer! Create a new named template that outputs the <tr>
element and call that template from <when> and <otherwise>.

// Magnus

Or just reorder the logic and use a variable:
<xsl:variable name="bgColor">
<xsl:choose>
<xsl:when test="$Rate &lt; 4">red</xsl:when>
<xsl:otherwise>lightblue</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<tr bgcolor="$bgColor">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
--
Joe Fawcett - XML MVP

https://mvp.support.microsoft.com/pr...8-8741D22D17A5
Dec 2 '05 #3
Hi Joe and Magnus,

Magnus, right now we already using ur approach....but it seems very
ugly...as lots of code is required and if a single change comes in
param then ll affect all subsiding templates....

But Joe..........u r simply gr8. I really like ur suggestion....

A many many thanks to both....

Thanks
Rushikesh

Dec 5 '05 #4
"Rushi" <IM***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi Joe and Magnus,

Magnus, right now we already using ur approach....but it seems very
ugly...as lots of code is required and if a single change comes in
param then ll affect all subsiding templates....

OK, if I where to implement this, I would not use a for-each instruction to
begin with. Match templates are much more flexible in the long run.
Something like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<html>
<head>My XSLT Testing</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="Books">
<table>
<tr bgcolor="BBCCAA">
<td>Book Name</td>
<td>Rating</td>
</tr>
<xsl:apply-templates/>
</table>
</xsl:template>

<xsl:template match="Book">
<tr>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="Rating &gt; 4">red</xsl:when>
<xsl:otherwise>lightblue</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<td>
<xsl:value-of select="Rating"/>
</td>
<td>
<xsl:value-of select="Name"/>
</td>
</tr>
</xsl:template>

</xsl:stylesheet>

// Magnus
Dec 5 '05 #5

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...
3
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By...
3
by: pradeep gummi | last post by:
I have an XML FILE that is to be converted to Plain Text using an XSL file. Since I just want plain text, I do not want to set any root element during transformation.And if I do not any root...
12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
6
by: Jain, Pranay Kumar | last post by:
Hi All, We have created a simple application that takes a dataset and generates the data in Excel schema format. It uses an xslt file to do the transformation for excel 2002 and so on. We are...
2
by: TomekR | last post by:
Hello ! I was developing xslt sheet lately and - experimenting - I made mistake resulting in that, the effect of the transformation is not well-formed xml document. I made these tests using...
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
15
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here...
2
by: Jeff Dege | last post by:
I'm working with a group that's been doing C++ coding for quite a long time, now, and in that environment we've pretty much worked out development practices that serve us well. We've been doing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.