bearu
bearu

Reputation: 1

Getting an image to display in XML and XSLT

I'm taking an intro to Computer Science course and I'm having a really hard time with this week's assignment. Essentially, I have to give data for four students and display an image of them in my table. I've been working on this for hours and its really starting to burn me out and make me wonder if these classes are meant for me. I was hoping someone would be able to help me.

XML:

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>

<class> 
    <student> 
        <name>Tim Jenkins</name> 
        <gradelvl>Freshman</gradelvl>
        <address>
        596 Riverwalk Ave
        Faketown, CA
        91234
        </address>
        <image
        source="tim.jpg">
        </image>
        <movie>Shutter Island (2010)</movie>
    </student>
    
     <student> 
        <name>Sadie Rowlin</name> 
        <gradelvl>Senior</gradelvl>
        <address>
        789 World St.
        Faketown, CA
        91234
        </address>
        <movie>She's the Man (2006)</movie>
    </student>
    
     <student> 
        <name>Jude Bowman</name> 
        <gradelvl>Senior</gradelvl>
        <address>
        909 High St.
        Faketown, CA
        91234
        </address> 
        <movie>The Shining (1980)</movie>
        
    </student>
    
     <student> 
        <name>Grady Cole</name> 
        <gradelvl>Sophomore</gradelvl>
        <address>
        1212 Pecan Rd.
        Faketown, CA
        91234
        </address>
        <movie>Donnie Darko (2001)</movie>      
    </student>
    
        
</class>

XSL:

<?xml version = "1.0" encoding = "UTF-8"?>

<xsl:stylesheet version = "1.0" 
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">   
   <xsl:template match = "/"> 
      <html> 
         <body> 
            <h2>Ms. Johnston's Film Class</h2> 
                
            <table border = "3"> 
            
               <tr bgcolor = "#9acd32"> 
                  
                  <th>Picture of Student</th>
                  <th>Name</th> 
                  <th>Grade Level</th>
                  <th>Address</th>
                  <th>Favorite Movies</th>
               
               </tr>
               
               <xsl:for-each select="class/student">
               
              <tr>  
                    <td><xsl:value-of select = "image"/></td>   

                    <td><xsl:value-of select = "name"/></td>
                    <td><xsl:value-of select = "gradelvl"/></td>                    
                    <td><xsl:value-of select = "address"/></td> 
                    <td><xsl:value-of select = "movie"/></td>                    
                  </tr> 
                  
               </xsl:for-each>  
            </table> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>

I feel like I've been staring at my assignment for hours and it's really frustrating and discouraging as a beginner. I'm essentially just trying to get the image for "Tim" to show up first. If I can see one example of how to do this, I could easily finish my homework. Please let me know for any insights!

Upvotes: 0

Views: 46

Answers (2)

Michael Kay
Michael Kay

Reputation: 163587

As with all problems, you need to break it into parts. In this case there are two parts:

(a) deciding what HTML you want to generate

(b) deciding what XSLT you need to write it order to generate it.

The first issue is an HTML problem not an XSLT problem. You need to generate something like <img src="tim.jpg"/> although you may need to expand the URI depending on where the image is actually stored.

The second issue is writing the XSLT to convert <image source="tim.jpg"></image> to <img src="tim.jpg"/>. That's simple:

<td><img src="{image/@source}"/></td>

Both steps are really quite easy, but I think your frustration probably arose because you didn't understand the concepts well enough to realise that you had to break it down into these two steps. At least, that's what I guess from the way you described the experience. Personally, I always find that once you've grasped the concepts, writing the code is easy. You've taken the objective of the task "just trying to get the image for "Tim" to show up", and you haven't worked out the intermediate step "I need to generate the following HTML".

Upvotes: 1

Ajeet Singh
Ajeet Singh

Reputation: 1076

only following Code change:-

<td><xsl:value-of select = "image/@source"/></td>

Upvotes: 0

Related Questions