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.
could do what you want. You could even include
doctype-system
anddoctype-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.XSLT is not designed for generating polyglot documents.
will always generate
without the closing slash.
may produce
or
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.
There is a dirty way for this: "fooling" the processor and producing a string
OK, I agree that it's an awful trick, but it works with all proc.
If you have an XSLT 2.0 processor you can specify XHTML for the output method, which should serialize the img element properly.
http://www.w3.org/TR/xslt-xquery-serialization/#xhtml-output
try if this helps