473,800 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Professio nal 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>Professio nal 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:styleshe et 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:valu e-of select="$Book/Name" /></td>
<td><xsl:valu e-of select="$Rate" /></td>
</tr>
</xsl:when>
<xsl:otherwis e>
<tr bgcolor="lightb lue">
<td><xsl:valu e-of select="$Book/Name" /></td>
<td><xsl:valu e-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....bu t the actual view remain same, like....

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

<td><xsl:valu e-of select="$Book/Name" /></td>
<td><xsl:valu e-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 1354
"Rushi" <IM***********@ gmail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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:valu e-of select="$Book/Name" /></td>
<td><xsl:valu e-of select="$Rate" /></td>
</tr>
</xsl:when>
<xsl:otherwis e>
<tr bgcolor="lightb lue">
<td><xsl:valu e-of select="$Book/Name" /></td>
<td><xsl:valu e-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************ ***@NOSPAMerics son.com> wrote in message
news:dm******** **@news.al.sw.e ricsson.se...
"Rushi" <IM***********@ gmail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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:valu e-of select="$Book/Name" /></td>
<td><xsl:valu e-of select="$Rate" /></td>
</tr>
</xsl:when>
<xsl:otherwis e>
<tr bgcolor="lightb lue">
<td><xsl:valu e-of select="$Book/Name" /></td>
<td><xsl:valu e-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="$bgCol or">
<td><xsl:valu e-of select="$Book/Name" /></td>
<td><xsl:valu e-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.goo glegroups.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:styleshe et 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:attribut e 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
2713
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...
3
6899
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 composition mainly? What're others? Thanks in advance for your comments!
3
7929
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 element during transformation, it return s "java.lang.IllegalStateException: Root element not set" exception. If I add any element for the enclosed root, it works. Note: I am using XMLOutputter object of JDOM API, packages javax.xml.transform and...
12
3239
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 (xml->html) doen't happen! I have XSLT files for this, they work, I mean when I put the output of the app as an XML file on some server, and make it use the XSLT files to transform into HTML, it works, I get a HTML page.
6
4546
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 using the dotnet.xml resource and not the MSXML 4. The application is golden and works fine with realtively Medium size of data(around 25MB). We started to see issues if the data is greater then 25 MB where the transformation takes tooo long and...
2
1794
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 XmlSpy and in output window I can see two parallel elements - according to "logic" of me sheet. The error of that document is that it doesn't have root element. Here is the contents of XmlSpy output window:
3
1835
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
2972
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 is how all the examples I've seen so far create an OleDbCommand Object: Dim cmd as new OleDbCommand("Select * FROM Table1",cnn) I had to figure out that it was the same as this:
2
1974
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 more and more, over the last few years, in C# and ASP.NET. Some web apps, some background services. In our C++ code base, we have a fair number of statically-linked libraries that contain code we share between projects. At this point, in our...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10501
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
10273
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
10250
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
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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
3764
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.