Project

Profile

Help

XSLT output and empty elements

Added by Anonymous over 16 years ago

Legacy ID: #4569844 Legacy Poster: Olivier (lordeagle)

Hi, Sorry if this issue has been addressed before, but I couldn't find it. Ok so I'm transforming XML + HTML (some tags allow well-formed html tags) into an XHTML document. So in my Stylesheet I used <xsl:output method="xhtml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> Now it works great, except that some tags that have an EMPTY content model (like img) are being output as <img......></img>. Note: the image tags causing a problem is created entirely from the XSL (i.e disregard the fact that my source XML allows HTML tags inside it). And just to give an idea, here's an extract from the Stylesheet (pretty plain simple). <div class="partnerMod-header"> <xsl:if test="logo"> <img> <xsl:attribute name="src"><xsl:value-of select="logo"/></xsl:attribute> <xsl:attribute name="alt"><xsl:value-of select="logo/@alt"/></xsl:attribute> <xsl:attribute name="width"><xsl:value-of select="logo/@width"/></xsl:attribute> <xsl:attribute name="height"><xsl:value-of select="logo/@height"/></xsl:attribute> </img> </xsl:if> </div> So any clues? PPS: this is the beginning of the stylesheet file: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:amt="urn:amt" exclude-result-prefixes="xs fn">


Replies (7)

Please register to reply

RE: XSLT output and empty elements - Added by Anonymous over 16 years ago

Legacy ID: #4570184 Legacy Poster: Michael Kay (mhkay)

I imagine that your <img> element is not in the XHTML namespace. The XHTML output method minimizes empty elements only for those elements that have an empty content model, and no element can have an empty content model unless it is in the XHTML namespace.

RE: XSLT output and empty elements - Added by Anonymous over 16 years ago

Legacy ID: #4570194 Legacy Poster: Olivier (lordeagle)

Hmmm I didn't encounter the problem with the Microsoft engine (I switched to Saxon because, you guessed it, I need XSLT and XPath 2), nor when I render my documents with the built-in Altova XML Spy engine... so how do I do this? How/where do I set up my output elements as belonging to the XHTML namespace? Thanks for your help Michael.

RE: XSLT output and empty elements - Added by Anonymous over 16 years ago

Legacy ID: #4570328 Legacy Poster: Michael Kay (mhkay)

XSLT 1.0 doesn't support the XHTML output method. So long as you are generating all elements using literal result elements, you can put them in the XHTML namespace by declaring this as the default namespace in your stylesheet. If you also use xsl:element and/or xsl:copy you will need to make further changes. Alternatively, since your stylesheet is generating HTML rather than XHTML, use the HTML output method and the output should render OK.

RE: XSLT output and empty elements - Added by Anonymous over 16 years ago

Legacy ID: #4570651 Legacy Poster: Olivier (lordeagle)

As mentionned (and as indicated in the stylesheet extract), I switched to Saxon because I need XSLT 2 features. So my stylesheet specifies version="2.0" and xsl:output method="xhtml" should work. However while writing this, I just noticed the following: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:amt="urn:amt" exclude-result-prefixes="xs fn"> Specifies version 2.0.... but then the output element: <xsl:output method="xhtml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> ...specifies version 1.0. Would that cause a "conflict"?

RE: XSLT output and empty elements - Added by Anonymous over 16 years ago

Legacy ID: #4571351 Legacy Poster: Michael Kay (mhkay)

version="1.0" on the xsl:output element indicates the version of XHTML, not the version of XSLT. In practice it has no effect, Saxon's XHTML output method is not version-sensitive. You just need to fix the namespace.

RE: XSLT output and empty elements - Added by Anonymous over 16 years ago

Legacy ID: #4572595 Legacy Poster: Olivier (lordeagle)

Hail Great Michael, So I'm almost there. Works fine, except that by setting the default namespace to XHTML (using xmlns="http://www.w3.org/1999/xhtml" in the xsl:stylesheet element) it makes my output also specify a default namespace (xmlns="http://www.w3.org/1999/xhtml"). Is that linked to the fact that my default namespace in the stylesheet is set to XHTML, or is it the XHTML method that forces that? Is it possible to remove this default namespace declaration? (it would simplify the process because the output is not sent directly to a web browser, but rather is parsed by our partner's platform for custom tags... and setting a default namespace makes things a bit more complicated in .Net -- i.e use a namespace manager, etc.) Thanks a lot. BTW, I've been looking around the web for an answer to this, and Mike you seem to be everywhere when it comes to stuff like that ;)

RE: XSLT output and empty elements - Added by Anonymous over 16 years ago

Legacy ID: #4572666 Legacy Poster: Michael Kay (mhkay)

Why are you using XHTML output method? (A question I often ask). If you want to send it to the browser, use HTML. If you want to send it to an application, use XML. As for your choice of namespace, you've got to agree it with the receiving application. If the agreement says they are expecting XHTML then it must be in the XHTML namespace (it shouldn't matter whether this is the default namespace or whether it uses an explicit prefix). If the agreement says they want it in no namespace, then do that. Whichever it is, it seems unlikely that the application requires the special XHTML serialization rules to be followed.

    (1-7/7)

    Please register to reply