Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using an XML value within an image tag

Status
Not open for further replies.

cric2k

Technical User
Jun 29, 2005
5
GB
Hi,
I'm completely new to xml and having a bit of hard time figuring this out.

In my xsl file im trying to use the value of an XML attribute within an image

my first attepmt was this

<img src="images/<xsl:value-of select="Country"/>.gif">

as you can see i'm trying to use the country value which I fetched from an external xml as the name of locally stored image files.

I'm used to writting in ASP code so please excuse my lame attempt.

any help would be great
 
Code:
<img src="images/{Country}.gif">


Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
You don't put it in any tag. Just put it where you want it to be output, eg:
Code:
<xsl:template match="/">
 <html>
  <head>
   <title>My page</title>
  </head>
  <body>
   <p>
    <img src="[URL unfurl="true"]http://www.xscores.com/f/{Country}.gif"[/URL] alt="{Country}"/>
   </p>
  </body>
 </html>
</xsl:template>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Hi,

that doesn't appear to work at all for me.
the image that appears on the site when uploaded is just linked to literally.

I've copied and pasted the code exactly as you have displayed and i get nothing.


Is where i have my test page, you can see that I am trying to change the items under country to their national flags to save space.

I have images like FINLAND.gif etc sitting in a local directory and i want to use images for these without a huge if statement.

cheers,
Michael
 
Post your ASP page, your XML and your XSL.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
ASP PAGE

Code:
<% pagetitle="Live Football Scores" %>
<!--#INCLUDE VIRTUAL="/includes/global_sql.asp"-->
<!--#INCLUDE VIRTUAL="/includes/header_sql.asp"-->
<br>
              <%
' this function takes two strings as an input and counts
' how many occurrences of string 2 are found in string 1
' It counts how many headlines are available, if you are
' using an organised category rather than a custom one you
' could delete this function.
Function CountOccurances(strOne, strTwo)

  Dim intCount, intCursor
  Do
    intCursor = InStr(intCursor + 1, strOne,strTwo)
    If intCursor > 0 Then
      intCount = intCount + 1
    End If
  Loop While intCursor > 0
  CountOccurances = intCount
End Function

' define the variables
dim objHTTP
dim objXML
dim objXSL

' set-up XMLHTTP

set objHTTP = Server.CreateObject("Microsoft.XMLHTTP")

' get the requested XML data from the remote location
' change the URL as per your feed.
objHTTP.open "GET", "[URL unfurl="true"]http://www.exactscores.com/SoccerLivescoreXml.xml",[/URL] false
objHTTP.send

' save the XML in objXML as XML
set objXML = objHTTP.responseXML

' set-up XMLDOM
set objXSL=Server.CreateObject("microsoft.xmldom")
objXSL.async=false

' load the XSL style sheet into the objXSL object
' change file name to match your XSL file
objXSL.load(Server.MapPath("csstyle.xsl"))

' check for errors in the XSL
if (objXSL.readyState = 4 AND objXSL.parseError.errorCode = 0) then

  ' check how many headlines there are by counting the TR tags
  ' delete this line if not using function, CountOccurances.
  HowMany = CountOccurances(objXML.transformnode(objXSL), "<tr>")

  ' delete the next three lines if not using function, CountOccurances.
  if (isnull(HowMany) or (HowMany ="")) then
    Response.Write "Sorry, no headlines available at present!"
  else
      ' if everything is alright parse the XML with our XSL
    Response.Write(objXML.transformnode(objXSL))
    ' delete the next line if not using function, CountOccurances.
  end if
else
  'if an error occurs, report it
  Response.Write "Error: " & objXSL.parseError.reason & "<br> URL:" & objXSL.url
end if
%>
<!--#INCLUDE VIRTUAL="/includes/footer.asp"-->
</quote>


and below is my XSL page

Code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/TR/WD-xsl">[/URL]
<xsl:template match="/">
<html>
<head>
<title>Sean Graham Live Scorers</title>
</head>
<body bgcolor="#ffffff">
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0" width="95%"  style="border: 1 solid #000" >
<tr bgcolor="000000">
<td><font color="ffffff">
Country</font>
</td>
<td><font color="ffffff">
KO</font>
</td>
<td><font color="ffffff">
Home Team</font>
</td>
<td><font color="ffffff">
Y</font>
</td>
<td><font color="ffffff">
R</font>
</td>
<td><font color="ffffff">
Away Team</font>
</td>
<td><font color="ffffff">
Y</font>
</td>
<td><font color="ffffff">
R</font>
</td>
<td><font color="ffffff">
Minute</font>
</td>
<td><font color="ffffff">
HT</font>
</td>
<td><font color="ffffff">
FT</font>
</td>
<td><font color="ffffff">
ET</font>
</td>
<td><font color="ffffff">
Pen.</font>
</td>
<td><font color="ffffff">
Scorers</font>
</td>
</tr>



 



<xsl:for-each select="SoccerLivescore/Item">

<xsl:choose>

<xsl:otherwise>
<tr>
<td align="left" style="BORDER-BOTTOM: #cccccc 1px solid;">
<b>



<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="Country"/><img src="[URL unfurl="true"]http://www.seangraham.com/livescores/{Country}.gif"[/URL] alt="{Country}"/>
</font>
</b>





</td>

<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="Start"/>
</font>
</td>


<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="HomeTeam"/>
</font>
</td>
<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="HomeYellowCards"/>
</font>
</td>
<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="HomeRedCards"/>
</font>
</td>


<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="AwayTeam"/>
</font>
</td>
<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="AwayYellowCards"/>
</font>
</td>
<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="AwayRedCards"/>
</font>
</td>



<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="Minute"/>
</font>
</td>

<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="HalfTime"/>
</font>
</td>


<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="FullTime"/>
</font>
</td>

<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="ExtraTime"/>
</font>
</td>

<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="Penalties"/>
</font>
</td>

<td style="BORDER-BOTTOM: #cccccc 1px solid;">
<font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
<xsl:value-of select="Scorers"/>
</font>
</td>

</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

the XML source i'm trying to use is here

sorry for being a pain, i've never touched XML before today and i'm only finding my feet.

Michael
 
Try this:
Code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0" xmlns:es="[URL unfurl="true"]http://www.exactscores.com">[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title>Sean Graham Live Scorers</title>
      </head>
      <body bgcolor="#ffffff">
        <table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0" width="95%" style="border: 1 solid #000">
          <tr bgcolor="000000">
            <td>
              <font color="ffffff">
Country</font>
            </td>
            <td>
              <font color="ffffff">
KO</font>
            </td>
            <td>
              <font color="ffffff">
Home Team</font>
            </td>
            <td>
              <font color="ffffff">
Y</font>
            </td>
            <td>
              <font color="ffffff">
R</font>
            </td>
            <td>
              <font color="ffffff">
Away Team</font>
            </td>
            <td>
              <font color="ffffff">
Y</font>
            </td>
            <td>
              <font color="ffffff">
R</font>
            </td>
            <td>
              <font color="ffffff">
Minute</font>
            </td>
            <td>
              <font color="ffffff">
HT</font>
            </td>
            <td>
              <font color="ffffff">
FT</font>
            </td>
            <td>
              <font color="ffffff">
ET</font>
            </td>
            <td>
              <font color="ffffff">
Pen.</font>
            </td>
            <td>
              <font color="ffffff">
Scorers</font>
            </td>
          </tr>
          <xsl:for-each select="es:SoccerLivescore/es:Item">
            <tr>
              <td align="left" style="BORDER-BOTTOM: #cccccc 1px solid;">
                <b>
                  <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                    <xsl:value-of select="es:Country"/>
                    <img src="[URL unfurl="true"]http://www.seangraham.com/livescores/{es:Country}.gif"[/URL] alt="{es:Country}"/>
                  </font>
                </b>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:Start"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:HomeTeam"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:HomeYellowCards"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:HomeRedCards"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:AwayTeam"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:AwayYellowCards"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:AwayRedCards"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:Minute"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:HalfTime"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:FullTime"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:ExtraTime"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:Penalties"/>
                </font>
              </td>
              <td style="BORDER-BOTTOM: #cccccc 1px solid;">
                <font face="verdana, arial, helvetica, sans-serif" size="-2" color="#000000">
                  <xsl:value-of select="es:Scorers"/>
                </font>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
It worked a treat!

thank you very much, that has been melting my head all day.

cheers,
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top