Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

xslt

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 ?

like image 506
Stephane Rolland Avatar asked Sep 02 '10 09:09

Stephane Rolland


People also ask

How can XSLT transform XML into HTML explain with example?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

How do you assign a value to XSLT variable?

XSLT <xsl:variable> The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

What is one way to add an attribute to an element in XSLT?

Attributes can be added or modified during transformation by placing the <xsl:attribute> element within elements that generate output, such as the <xsl:copy> element. Note that <xsl:attribute> can be used directly on output elements and not only in conjunction with <xsl:element> .

Which symbol is used to get the element value in XSLT?

The <xsl:value-of> element is used to extract the value of a selected node.


2 Answers

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.

like image 194
musiKk Avatar answered Sep 22 '22 18:09

musiKk


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>

like image 42
Piotr Nawrot Avatar answered Sep 23 '22 18:09

Piotr Nawrot