[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Wrting an image link with an XSLT

Posted on 2011-10-17
4
Medium Priority
?
240 Views
Last Modified: 2012-06-27
I'm calling the following XML feed;

http://www.google.co.uk/ig/api?weather=sydney

And thanks to some great help from Gertone transforming it with the following XSLT

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

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/*">
        <xsl:apply-templates select="node()"/>
    </xsl:template>

    <xsl:template match="*">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(name(),'_data')"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()|@data"/>
        </div>
    </xsl:template>
    
    <xsl:template match="forecast_information|current_conditions|forecast_conditions|icon|forecast_date">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="name()"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()|@data"/>
        </div>
    </xsl:template>
    
    <xsl:template match="postal_code|latitude_e6|longitude_e6|temp_f"/>

    <xsl:template match="low | high">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="concat(name(), '_data')"/>
            </xsl:attribute>
            <xsl:call-template name="farenheit-to-celsius">
                <xsl:with-param name="temp" select="@data"/>
            </xsl:call-template>
        </div>
    </xsl:template>
    
    <xsl:template match="*" mode="id">
        <xsl:value-of select="concat(name(),'_data')"/>
    </xsl:template>
    
    <xsl:template name="farenheit-to-celsius">
        <xsl:param name="temp"/>
        <xsl:choose>
            <xsl:when test="not(string(number($temp)) = 'NAN')">
                <xsl:value-of select="format-number(($temp - 32) * (5 div 9) , '##.0')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$temp"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Open in new window


For the node where it renders

<div class="icon">/ig/images/weather/mostly_sunny.gif</div>

Open in new window


I'd like it to render

<div class="icon"><img src="/myfolderpath/ig/images/weather/mostly_sunny.gif"></div>

Open in new window

0
Comment
Question by:sonic1234
  • 3
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 36978775
add this

    <xsl:template match="icon">
        <div>
            <xsl:attribute name="class">
                <xsl:value-of select="name()"/>
            </xsl:attribute>
           <img src="/myfolderpath/{@data}"/>
        </div>
    </xsl:template>

and remove icon from the existing template,
change line 19 from
    <xsl:template match="forecast_information|current_conditions|forecast_conditions|icon|forecast_date">

into
    <xsl:template match="forecast_information|current_conditions|forecast_conditions|forecast_date">
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36978786
maybe you need to explicitely set the output method to html

XSLT needs to be wellformed (hence the element closing for <img/>)

XML : <img ... />

html doesn't want that

HTML : <img ... >

set the method to html to get around that

   <xsl:output indent="yes"/>
becomes
   <xsl:output indent="yes" method="html"/>
0
 

Author Closing Comment

by:sonic1234
ID: 36978801
Top class help Gertone, your clear code is helping me learn how this works.  Much appreciated.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36978990
Welcome
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Integration Management Part 2
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question