BeachRunnerFred
BeachRunnerFred

Reputation: 18578

Simple XSLT Question: How can I add the closing "/" at the end of a single tag?

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!

Upvotes: 3

Views: 2113

Answers (2)

carillonator
carillonator

Reputation: 4743

You could also do:

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

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

Upvotes: 1

Andy Gherna
Andy Gherna

Reputation: 2165

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.

Upvotes: 5

Related Questions