Welcome to WebmasterWorld Guest from 3.233.226.151

Forum Moderators: open

Message Too Old, No Replies

Inserting images using XSLT

Inserting images using XSLT

     
2:08 am on Sep 17, 2005 (gmt 0)

New User

10+ Year Member

joined:July 25, 2005
posts:11
votes: 0


Hello all, quick question. I am transitioning our web content into xml files. I am able to use xslt to transform all of our stored content except our images.

I keep the path to the image in the xml file like this:

<top_photo1>/website/images/ExtView3.jpg</top_photo1>

and I am not sure what xslt syntax to use to transform it into the appropriate html i.e.:
<img style="float:left; margin-right:0px; margin-bottom:0px" src="/website/images/ExtView3.jpg" alt="=" title="Home" />

Currently I have tried many variations of this but none work:
<xsl:template match="/">
...
<img stlye="float:left; margin-right:0px; margin-bottom:0px" src="{top_photo1}" alt="=" title="Home" />
...
</xsl:template>
</xsl:transform>

Please note, all my other data transforms perfectly just not the images.

**Any pointers would be greatly appreciated.

6:54 pm on Sept 18, 2005 (gmt 0)

New User

10+ Year Member

joined:May 17, 2005
posts:37
votes: 0


It looks OK to me but it is probably a issue with the image src tag.

When you run the transform in your web browser and examine the HTML source that is generated, what is the URL to the image in the img src tag?

Is the src tag completely empty, or does it have a duff URL?

If empty, I'd suggest playing about with the XPATH in your <xsl:template match="/"> or possibly trying a syntax like this until you isolate the problem:

<img style="..." alt="="><xsl:attribute name="src"><xsl:value-of select="//top_photo1" /></xsl:attribute></img>

11:03 pm on Sept 19, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 17, 2001
posts:1262
votes: 0


What is the structure of your XML? With {top_photo1} you are requesting the text content of a child of the current node named <top_photo1>; however, your template is matching on the root (/) so you will almost certainly be getting null.

If for example your XML looked like

<article>
<date>19 Sep 2005</date>
<header>
<top_photo1>/website/images/ExtView3.jpg</top_photo1>
<author>Jane Doe</author>
</header>
<body>
<p>Main body of the article here.</p>
</body>
</article>

you could write

<xsl:template match="/">
...
<img style="float:left; margin-right:0px; margin-bottom:0px" src="{article/header/top_photo1}" alt="" title="Home" />
...
</xsl:template>

or for more flexibility perhaps

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
...
<xsl:template match="header">
<img style="float:left; margin-right:0px; margin-bottom:0px" src="{top_photo1}" alt="" title="Home" />
</xsl:template>
...

12:46 am on Sept 24, 2005 (gmt 0)

New User

10+ Year Member

joined:July 25, 2005
posts:11
votes: 0


Sorry guys I was out of town for the week. I will experiment with your suggestions and post up the results.

Thanks!