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">
<xslutput method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<xsl:template name="format-book">
<xslaram name="Rate" />
<xslaram name="Book" />
<xsl:choose>
<xsl:when test="$Rate < 4">
<tr bgcolor="red">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:when>
<xsltherwise>
<tr bgcolor="lightblue">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsltherwise>
</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 < 4">
<tr bgcolor="red">
</xsl:when>
<xsltherwise>
<tr bgcolor="lightblue">
</xsltherwise>
</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
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">
<xslutput method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<xsl:template name="format-book">
<xslaram name="Rate" />
<xslaram name="Book" />
<xsl:choose>
<xsl:when test="$Rate < 4">
<tr bgcolor="red">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsl:when>
<xsltherwise>
<tr bgcolor="lightblue">
<td><xsl:value-of select="$Book/Name" /></td>
<td><xsl:value-of select="$Rate" /></td>
</tr>
</xsltherwise>
</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 < 4">
<tr bgcolor="red">
</xsl:when>
<xsltherwise>
<tr bgcolor="lightblue">
</xsltherwise>
</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