XSL transformation not works for code reuse

Discussion in 'XML' started by Rushi, Dec 1, 2005.

  1. Rushi

    Rushi Guest

    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
     
    Rushi, Dec 1, 2005
    #1
    1. Advertising

  2. "Rushi" <> wrote in message
    news:...
    > <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
     
    Magnus Henriksson, Dec 2, 2005
    #2
    1. Advertising

  3. Rushi

    Joe Fawcett Guest

    "Magnus Henriksson" <> wrote in message
    news:dmoti0$bqp$...
    > "Rushi" <> wrote in message
    > news:...
    >> <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
    >
    >

    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>


    --
    Joe Fawcett - XML MVP

    https://mvp.support.microsoft.com/profile=8AA9D5F5-E1C2-44C7-BCE8-8741D22D17A5
     
    Joe Fawcett, Dec 2, 2005
    #3
  4. Rushi

    Rushi Guest

    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
     
    Rushi, Dec 5, 2005
    #4
  5. "Rushi" <> wrote in message
    news:...
    > 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
     
    Magnus Henriksson, Dec 5, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    5
    Views:
    552
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  2. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    432
    Hylander
    Feb 26, 2004
  3. Replies:
    0
    Views:
    548
  4. code reuse and design reuse

    , Feb 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,048
    Malcolm
    Feb 12, 2006
  5. jacob navia

    To reuse or not to reuse

    jacob navia, Nov 5, 2006, in forum: C Programming
    Replies:
    19
    Views:
    555
    Dave Thompson
    Dec 18, 2006
Loading...

Share This Page