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=开发者_如何学Go"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.
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"><img src="</xsl:text>
<xsl:value-of select="@src" />
<xsl:text disable-output-escaping="yes">" /></xsl:text>
</xsl:for-each>
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.
<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" />
<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.
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.
<xsl:output method="html" />
try if this helps
精彩评论