Stephane Rolland
Stephane Rolland

Reputation: 39926

Xslt generating Html <IMG> tags. How to use an XML node value as an src-attribute for the <IMG> Tags

I'm still searching, but I haven't found yet the way to perform something like this:

xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- some other templates -->
    <xsl:template match="IMAGE">
        <img src="src_attribute_to_be_read_from_the_xml_file.jpg"/>
    </xsl:template>     
</xsl:stylesheet>

In my Xml <IMAGE> tags, the text value is the filename that should be inserted in the string src_attribute_to_be_read_from_the_xml_file.jpg when processed by this Xslt file.

Have you any idea to perform this ?

Upvotes: 11

Views: 45193

Answers (3)

Pathros
Pathros

Reputation: 10740

And if you want to add the height, width, and alt attributes, then you can do it like the following:

         <img>
             <xsl:attribute name="src">
                 <xsl:value-of select="picture"/>
              </xsl:attribute>
              <xsl:attribute name="title">
                 <xsl:value-of select="pictureTitle"/>
              </xsl:attribute >
              <xsl:attribute name="alt">
                 <xsl:value-of select="pictureTitle"/>
              </xsl:attribute >
              <xsl:attribute name="height">
                 20
              </xsl:attribute >
              <xsl:attribute name="width">
                 30
              </xsl:attribute >
         </img>

Upvotes: 1

Piotr Nawrot
Piotr Nawrot

Reputation: 445

Alternatively you can use XSL template:

<xsl:template match="image">
<xsl:element name="IMG">
  <xsl:attribute name="src">
    <xsl:value-of select="your_path"/>
  </xsl:attribute>
  <xsl:attribute name="title">
    <xsl:value-of select="your_title"/>
   </xsl:attribute >
</xsl:element>

Upvotes: 3

musiKk
musiKk

Reputation: 15189

You use xsl:attribute:

<img>
    <xsl:attribute name="src">
        <xsl:value-of select="you path here"/>
    </xsl:attribute>
</img>

It should also be possible to use

<img src="{some expression here}" />

According to the specification this is called a attribute value template and should always work (i.e. both XSLT 1.0 and 2.0). Nice. Now I learned something too.

Upvotes: 36

Related Questions