XSL transformation not works for code reuse

R

Rushi

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:eek:utput 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:eek:therwise>
<tr bgcolor="lightblue">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:eek:therwise>
</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:eek:therwise>
<tr bgcolor="lightblue">
</xsl:eek:therwise>
</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
 
M

Magnus Henriksson

Rushi said:
<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:eek:therwise>
<tr bgcolor="lightblue">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:eek:therwise>
</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
 
J

Joe Fawcett

Magnus Henriksson said:
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:eek:therwise>lightblue</xsl:eek:therwise>
</xsl:choose>
</xsl:variable>

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

Rushi

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
 
M

Magnus Henriksson

Rushi said:
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:eek:utput 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:eek:therwise>lightblue</xsl:eek:therwise>
</xsl:choose>
</xsl:attribute>
<td>
<xsl:value-of select="Rating"/>
</td>
<td>
<xsl:value-of select="Name"/>
</td>
</tr>
</xsl:template>

</xsl:stylesheet>



// Magnus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top