开发者

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 ?


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.


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜