Jump to content

Image tag/foreach


Guest FirefoxRocks
 Share

Recommended Posts

Guest FirefoxRocks

Ok I have successfully completed my first XSLT + PHP + XML project!! You can see it here at http://dhseagles.kpdsb.on.ca/~athletics/ca...alendar_xml.php.Now I am working on my second one, and I have a little trouble with the image tag.Here is my XML file:

<?xml version="1.0" encoding="iso-8859-1"?><?xml-stylesheet type="text/xsl" href="roster.xsl"?><player><name>PLAYER NAME</name><age>PLAYER AGE</age><height>PLAYER HEIGHT</height><weight>PLAYER WEIGHT</weight><kills>PLAYER NUMBER OF KILLS</kills><image>PLAYER IMAGE URL (http://dhseagles.kpdsb.on.ca/blahblahblah.jpg)</image></player>

Here is my XSLT file:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body>   <h1><xsl:value-of select="player/name"/></h1><table><caption><xsl:value-of select="player/name"/></caption><tbody><tr><td> Age </td><td> <xsl:value-of select="player/age"/> </td><td rowspan="6"><img src="<xsl:value-of select='player/image'/>" alt="<xsl:value-of select='player/name'/>" /></td></tr><tr><td> Weight </td><td> <xsl:value-of select='player/height'/> </td></tr><tr><td> Height </td><td> <xsl:value-of select='player/weight'/> </td></tr><tr><td> Kills </td><td> <xsl:value-of select='player/kills'/> </td></tr></tbody></table> </body> </html></xsl:template></xsl:stylesheet>

That is invalid because there are tags inside of the attributes. So how can I do this?

Link to comment
Share on other sites

[...] That is invalid because there are tags inside of the attributes. So how can I do this?
Instead of trying to write the tag characters from your stylesheet, write element nodes. The tag characters are then created for you in the output:
<xsl:element name="img">  <xsl:attribute name="src">	<xsl:value-of select="player/image"/>  </xsl:attribute>  <xsl:attribute name="alt">	<xsl:value-of select="player/name"/>  </xsl:attribute></xsl:element>

Link to comment
Share on other sites

If you want, you can also use Attribute Value Templates (AVTs) like so:

<img src="{player/image}" alt="{player/name}"/>

Use the method Reg Edit showed above if you want a certain attribute to appear only on certain conditions (i.e. use <xsl:if/> or <xsl:choose/> around the <xsl:attribute/>) or if you want the name of the attribute to vary (using AVT inside the "name" attribute of the <xsl:attribute/> element).

Link to comment
Share on other sites

Guest FirefoxRocks
If you want, you can also use Attribute Value Templates (AVTs) like so:
<img src="{player/image}" alt="{player/name}"/>

Use the method Reg Edit showed above if you want a certain attribute to appear only on certain conditions (i.e. use <xsl:if/> or <xsl:choose/> around the <xsl:attribute/>) or if you want the name of the attribute to vary (using AVT inside the "name" attribute of the <xsl:attribute/> element).

Thanks. That is what I needed to make the stylesheet work. Now where in W3Schools did you learn that?
Link to comment
Share on other sites

Thanks. That is what I needed to make the stylesheet work. Now where in W3Schools did you learn that?
Nowhere. As the XSLT FAQ (which BTW answers your quesiton) this is the greatest loss of the W3Schools tutorial. I learned the basic concept first from this forum (that lesson is one of the main reasons I keep visiting this place... every now and then I learn something new that's not on W3Schools) and then after finding Dave Pawson's FAQ (link at the bottom of the topic above), I got a more general overview of the concept i.e. before then I thought you need to use variables inside AVTs, whereas now I know they accept any XPath expression.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...