tags:

views:

248

answers:

5

Well I want to render a self closing tag say <img> tag like this <img src="xyz.jpg" />

But I don't know how to do that...I mean how to render a self closing tag. What I'm having so far is below:-

Here is the XML:

<c:Image src="xyz.jpg"></c:Image>

And here is the XSLT:

<xsl:output indent="yes" omit-xml-declaration="yes" method="html"  />
.
.
.
    <xsl:for-each select="c:Image">
     <img>
      <xsl:attribute name="src">
        <xsl:if test="string-length(@src)>0">
          <xsl:text></xsl:text>
          <xsl:value-of select="@src"/>
        </xsl:if>
        </xsl:attribute>
     </img>
    </xsl:for-each>
.
.
.

Any help appreciated.

+1  A: 
<xsl:output method="html" version="4.0" />

could do what you want. You could even include doctype-system and doctype-public attributes to output a certain HTML DOCTYPE. See the documentation of <xsl:output>.

If you do not want to output HTML, but XML, you are a bit at a loss, I'm afraid.
<img></img> and <img /> are semantically equivalent, and the XSLT processor can choose eiher variant. You should not care too much.

Tomalak
Sorry...for not mentioning one of the important ingredient of the question. I have included it in the question now. Any Ideas now??
Manish
In this case - I'd say it is XSLT processor dependent as well. When tested with msxsl.exe, the `<img>` tag is output as implicitly closed - no `/>` and no `</img>` is generated.
Tomalak
BTW - `omit-xml-declaration` is redundant with `method="html"`.
Tomalak
Yeah, my Hardware XSLT Accelerator closes the tag as well. Tomalak is right, it is proc dependent.
Thiyagaraj
A: 
<xsl:output method="html" />

try if this helps

Ismail
It was already there in my code..sorry for not mentioning here. I did edited my question..now any ideas....
Manish
+1  A: 

There is a dirty way for this: "fooling" the processor and producing a string

<xsl:for-each select="c:Image">
  <xsl:text disable-output-escaping="yes">&lt;img src="</xsl:text>
  <xsl:value-of select="@src" />
  <xsl:text disable-output-escaping="yes">" /&gt;</xsl:text>
</xsl:for-each>

OK, I agree that it's an awful trick, but it works with all proc.

Erlock
No it doesn't work with all processors. Implementation of disable-output-escaping is optional for XSLT processors.
Alohci
You're right, but I didn't yet meet a "mainstream" processor that doesn't support this feature. Do you know one?
Erlock
I've used one in the past, but I don't recall what it was now. I don't know of any in current use. I haven't tested this, but I wonder if it would work if the XSLT was applied in the browser, since to work the output must be serialized and re-parsed, rather than the output tree simply becoming the DOM directly. Do you know if it works in all the well-known browsers?
Alohci
Thank God you added it by yourself that it's a dirty way or else you could have got many negative votes !
Manish
@Alohci: It works with IE, but not with FF. FF developers choosed to not implement it, at the despair of many developers. Disable output escaping is a useful feature for any purpose other than this one ;)
Erlock
Actually, `disable-output-escaping` is such a horrible feature and so prone to abuse that it was seriously considered for removal in XSLT 2.0, and was only retained for reasons of backwards compatibility: http://www.w3.org/TR/2007/REC-xslt20-20070123/#d-o-e-in-data-model
NickFitz
+1  A: 

XSLT is not designed for generating polyglot documents.

<xsl:output method="html" />

will always generate

<img src="xyz.jpg">

without the closing slash.

<xsl:output method="xml" />

may produce

<img src="xyz.jpg" />

or

<img src="xyz.jpg"></img>

depending on the processor.

In fact, browsers will do the right thing with any of these, but sending xslt generated xhtml to browsers with the text/html content type is likely to cause problems as tags for non-empty elements like <title />, <script />, <a /> etc, can easily be produced which browsers will misinterpret causing serious rendering problems.

You have to decide whether you want html or xhtml to be generated, and send with the appropriate content type (application/xhtml+xml for xhtml - not supported in IE), or post-process your xslt output to ensure that self closing tags are only used for canonically empty elements.

Alohci
A: 

If you have an XSLT 2.0 processor you can specify XHTML for the output method, which should serialize the img element properly.

<xsl:output method="xhtml" />

http://www.w3.org/TR/xslt-xquery-serialization/#xhtml-output

Given an XHTML element whose content model is EMPTY, the serializer MUST use the minimized tag syntax, for example <br />, as the alternative syntax <br></br> allowed by XML gives uncertain results in many existing user agents. The serializer MUST include a space before the trailing />, e.g. <br />, <hr /> and <img> src="karen.jpg" alt="Karen" />

Mads Hansen