tags:

views:

228

answers:

2

Hello, I'm teaching myself XSLT and am still trying to wrap my brain around the small details. I have a template in my XSL stylesheet that looks like this...

<td><img src="{normalize-space(image)}"/></td>

which produces XHTML that looks like this...

<td><img src="somefile.jpg"></td>

How do I change my XSL template to add a trailing "/" to the img tag so that the XHTML output looks like this...

<td><img src="somefile.jpg"/></td>

Also, why does the trailing slash in the template get omitted in the output?

Thanks in advance for all your help!

+4  A: 

I would start with this:

<xsl:output method="xml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
   doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" indent="yes"/>

This will get your XSL parser to close empty tags correctly since you are (technically) producing XML. HTML does not have the concept of an empty tag like XML/XHTML does.

Andy Gherna
thanks! i was getting an that said "The server did not understand the request, or the request was invalid. Error processing resource 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd". So I removed the doctype-system attribute and now it fixed the problem with omitting the '/', but now I'm receiving an error on line 2 of the output XHTML "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" that says "Required white space was missing". Your thoughts? Thanks again for your help!
BeachRunnerJoe
Try removing the doctype-system attribute and doctype-public for a start. If your output doesn't need to go through a validator, this should be OK.
Andy Gherna
A: 

You could also do:

<xsl:element name="img">
    <xsl:attribute name="src">blarg</xsl:attribute>
</xsl:element>

which will output <img src="blarg"/>

carillonator