Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Apply image to element...i think

    Hi everyone. Hope u all good.
    Im completely new to xml xslt. I got a xml document to do with movies. Where a movie is rated ="12" or "15" i want to add an image. So for instance if the movie was rated "12" i want the xsl code to apply a pg image to it.
    Hope ive made it clear enough for anyone willing to help. thanx. The codes are below

    XML

    <?xml version="1.0" ?>

    <?xml-stylesheet type="text/xsl" href="movies.xsl"?>

    <movies>

    <movie rating="12">
    <title>The Man They Call MySQL</title>
    <star>Rob Charles</star>
    <genre>Drama</genre>
    <format>DVD</format>
    <director>Martin Vilevic</director>
    <price>5.78</price>
    <released>2010</released>
    <review>Good movie</review>
    </movie>


    <movie rating="PG">
    <title>Ray The Fish</title>
    <star>Anna Roberts</star>
    <genre>Animation</genre>
    <format>Blu-Ray</format>
    <director>Jane Jones</director>
    <price>12.99</price>
    <released>2000</released>
    <review>Must see for kids</review>
    </movie>


    <movie rating="12">
    <title>Rise of The Robots</title>
    <star>Ricardo Melo</star>
    <genre>Action</genre>
    <format>DVD</format>
    <director>Greg Baker</director>
    <price>11.00</price>
    <released>2013</released>
    <review>No review yet</review>
    </movie>


    <movie rating="15">
    <title>With Love From Granite City</title>
    <star>Kate Van Guzman</star>
    <genre>Romance</genre>
    <format>DVD</format>
    <director>Carlos Domuingez</director>
    <price>12.00</price>
    <released>2011</released>
    <review>Good movie</review>
    </movie>

    </movies>

    XSL

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml">

    <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body style="background-color:silver">
    <xsl:for-each select="movies/movie">
    <xsl:sort select="title"/>
    <div style="width:450px; padding:5px; margin-bottom:10px; border:5px double black; color:black; background-color:white; text-align:left">
    <tr>
    <td><xsl:apply-templates select="title" /></td>
    </tr>
    <td><xsl:apply-templates select="star" /></td>
    <tr>
    <td><xsl:apply-templates select="genre" /></td>
    </tr>
    <tr>
    <td><xsl:apply-templates select="format" /></td>
    </tr>
    <tr>
    <td><xsl:apply-templates select="director" /></td>
    </tr>
    <tr>
    <td><xsl:apply-templates select="price" /></td>
    </tr>
    <tr>
    <td><xsl:apply-templates select="released" /></td>
    </tr>
    <tr>
    <td><xsl:apply-templates select="review" /></td>
    </tr>

    </div>
    </xsl:for-each>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="title">
    <div style="font-family:Verdana, Arial; font-size:18pt; font-weight:bold">
    <xsl:value-of select="."/>
    </div>
    </xsl:template>

    <xsl:template match="phone/voice">
    <div style="font-family:Verdana, Arial; font-size:14pt">
    <img src="pg.png" alt="Voice Phone" />
    <xsl:value-of select="."/>
    </div>
    </xsl:template>

    <xsl:template match="star">
    <div style="font-family:Verdana, Arial; font-size:14pt; font-weight:bold">
    <xsl:value-of select="."/>
    </div>
    </xsl:template>

    </xsl:stylesheet>

    Thank u all so much. Been playing with this for hours with no luck. frustrated

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    @teddy, I know we can do this with a
    <xsl:choose>
    <xsl:when, but ran into problems. This will work and since it's just two things we're choosing between it shouldn't hurt:

    The xsl:
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml">
    
    <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body style="background-color:silver">
    <xsl:for-each select="movies/movie">
    	<xsl:sort select="title"/>
    	<div style="width:450px; padding:5px; margin-bottom:10px; border:5px double black; color:black; background-color:white; text-align:left">
    	<tr>
    	<td>
    <xsl:if test="@rating = 12">
    	<img src="pg.jpg" alt="Voice Phone"></img>
    </xsl:if>
    
    <xsl:if test="@rating = 15">
    	<img src="pg.jpg" alt="Voice Phone"></img>
    </xsl:if>
    <xsl:apply-templates select="title" />
    	</td>
    	</tr>
    	<td><xsl:apply-templates select="star" /></td>
    	<tr>
    	<td><xsl:apply-templates select="genre" /></td>
    	</tr>
    	<tr>
    	<td><xsl:apply-templates select="format" /></td>
    	</tr>
    	<tr>
    	<td><xsl:apply-templates select="director" /></td>
    	</tr>
    	<tr>
    	<td><xsl:apply-templates select="price" /></td>
    	</tr>
    	<tr>
    	<td><xsl:apply-templates select="released" /></td>
    	</tr>
    	<tr>
    	<td><xsl:apply-templates select="review" /></td>
    	</tr>
    	</div>
    </xsl:for-each>
    </body>
    </html>
    </xsl:template>
    
    <xsl:template match="title">
    <div style="font-family:Verdana, Arial; font-size:18pt; font-weight:bold">
    <xsl:value-of select="."/>
    </div>
    </xsl:template>
    
    <xsl:template match="phone/voice">
    <div style="font-family:Verdana, Arial; font-size:14pt">
    <img src="pg.jpg" alt="Voice Phone" />
    <xsl:value-of select="."/>
    </div>
    </xsl:template>
    
    <xsl:template match="star">
    <div style="font-family:Verdana, Arial; font-size:14pt; font-weight:bold">
    <xsl:value-of select="."/>
    </div>
    </xsl:template>
    
    </xsl:stylesheet>
    Evolution - The non-random survival of random variants.

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by sunfighter View Post
    The xsl:
    Code:
    <xsl:if test="@rating = 12">
    	<img src="pg.jpg" alt="Voice Phone"></img>
    </xsl:if>
    
    <xsl:if test="@rating = 15">
    	<img src="pg.jpg" alt="Voice Phone"></img>
    </xsl:if>
    With a tweak, your code can be made less redundant:

    Code:
    <xsl:if test="@rating = 12 or @rating = 15">
    	<img alt="Voice Phone" width="?" height="?" src="pg.jpg"/>
    </xsl:if>
    Quote Originally Posted by sunfighter View Post
    @teddy, I know we can do this with a
    <xsl:choose>
    <xsl:when, but ran into problems.
    Not sure what those would be. choose/when is virtually identical in this case aside from the extra choose element:

    Code:
    <xsl:choose>
    	<xsl:when test="@rating = 12 or @rating = 15">
    		<img alt="Voice Phone" width="?" height="?" src="pg.jpg"/>
    	</xsl:when>
    </xsl:choose>
    Quote Originally Posted by teddy77 View Post
    <html xmlns="http://www.w3.org/1999/xhtml">
    This is redundant. You already declared the default namespace. Declaring it again has no effect on the output.

    Quote Originally Posted by teddy77 View Post
    <div style="width:450px; padding:5px; margin-bottom:10px; border:5px double black; color:black; background-color:white; text-align:left">
    <tr>
    You're missing a table element here. It's a moot point though, I suppose; a one-column table indicates that you should be displaying this content via an unordered list (ul element) instead.

    Quote Originally Posted by teddy77 View Post
    <xsl:template match="title">
    <div style="font-family:Verdana, Arial; font-size:18pt; font-weight:bold">
    <xsl:value-of select="."/>
    </div>
    </xsl:template>

    <xsl:template match="phone/voice">
    <div style="font-family:Verdana, Arial; font-size:14pt">
    <img src="pg.jpg" alt="Voice Phone" />
    <xsl:value-of select="."/>
    </div>
    </xsl:template>

    <xsl:template match="star">
    <div style="font-family:Verdana, Arial; font-size:14pt; font-weight:bold">
    <xsl:value-of select="."/>
    </div>
    </xsl:template>
    Even when using an XSLT style sheet, you're still supposed to use semantically correct elements like h1 and h2. You may not have even had to bother with some of the styles for this development code if you had relied on the default styles of those elements.

    Also, using a CSS style sheet will save you some redundancy and make it easier to change styles at a later point.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    Quote Originally Posted by Arbitrator View Post
    With a tweak, your code can be made less redundant:

    Code:
    <xsl:if test="@rating = 12 or @rating = 15">
    	<img alt="Voice Phone" width="?" height="?" src="pg.jpg"/>
    </xsl:if>
    Not sure what those would be. choose/when is virtually identical in this case aside from the extra choose element:

    Code:
    <xsl:choose>
    	<xsl:when test="@rating = 12 or @rating = 15">
    		<img alt="Voice Phone" width="?" height="?" src="pg.jpg"/>
    	</xsl:when>
    </xsl:choose>
    Thank you for that Arbitrator I used the first code block with an and type 'test' and that has always worked. I just subsittuded an or and it broke the code. [Everything had an image] Played with it for a while and got no where so went with what I know.
    Now I know something more
    Evolution - The non-random survival of random variants.

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for this post right now i am learning xml and i hope it would help me a lot.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •