Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


XSL transformation not works for code reuse

From: "Rushi" <IMS.Rushikesh@-----.--->
To: NULL
Date: 12/1/2005 1:22:00 AM
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



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent