How can I use xsl:value-of in image path <img src = "<xsl:value-of>" />
Using XSLT, I am drawing data from a SQL database. The thumbnail column returns the end of an image path - photo_thumbnail.jpg. I call the thumbnail in an <xsl:value-of>, and I am trying to append this thumbnail data to the end of an image path:
but I get an error. I understand that < > can't be part of an image path, but I'm not sure what syntax changes I need to make this work. I am going about this the wrong way? How can I include <xsl:value-of> as part of a file path?
* Generally, use the curly braces like you use <xsl:value-of /> but output goes inside an attribute value (they're called an "Attribute Value Template"), e.g.:
<a href="{url}">link</a>
* Use the <xsl:attribute> version when you need to do complicated logic determining the value; If you don't know the name of the attribute or if you don't always want the attribute added:
* Never use the <xsl:text disable-output-escaping="yes"> with CDATA hack - it's meant to be so ugly and verbose so it works as a flag that says "this is wrong, wrong, wrong" :-)
Regarding the {file} thing: Whatever you write in there is effectively an XPath expression, so if you're inside a template that matches an Image element (nodes in the Media section looks like that), you can access the child nodes using their aliases, like this:
How can I use xsl:value-of in image path <img src = "<xsl:value-of>" />
Using XSLT, I am drawing data from a SQL database. The thumbnail column returns the end of an image path - photo_thumbnail.jpg. I call the thumbnail in an <xsl:value-of>, and I am trying to append this thumbnail data to the end of an image path:
<img src = "/folder/images/<xsl:value-of select='thumbnail' />" />
but I get an error. I understand that < > can't be part of an image path, but I'm not sure what syntax changes I need to make this work. I am going about this the wrong way? How can I include <xsl:value-of> as part of a file path?
Hey Luke,
Try something like
Rich
Hi Luke,
try this:
Peter
I managed to rough it out with CDATA:
But it is much cleaner to use the attribute method you suggested. Thanks!
You could also do it:
Richard
Above is perfectly correct, or you could simplify it further too...
<img src="/path/{file}" alt="" />
Curly brackets allow you to pass a value into a attribute (e.g src, alt, href). Hehe don't forgot the ALT tag ;)
Laurie : - }
Interesting!
Laurie, if I use {file} in the path, I assume I would need to declare it as a variable?
Something like this?
<xsl:variable name = "file"><xsl:value-ofselect='thumbnail'/></xsl:variable>
Hi Luke,
Here's a couple of rules of thumb:
* Generally, use the curly braces like you use <xsl:value-of /> but output goes inside an attribute value (they're called an "Attribute Value Template"), e.g.:
* Use the <xsl:attribute> version when you need to do complicated logic determining the value; If you don't know the name of the attribute or if you don't always want the attribute added:
* Never use the <xsl:text disable-output-escaping="yes"> with CDATA hack - it's meant to be so ugly and verbose so it works as a flag that says "this is wrong, wrong, wrong" :-)
Regarding the {file} thing: Whatever you write in there is effectively an XPath expression, so if you're inside a template that matches an Image element (nodes in the Media section looks like that), you can access the child nodes using their aliases, like this:
(So you don't have to create a variable).
/Chriztian
is working on a reply...