Project

Profile

Help

<xsl:comment> not indenting?

Added by Anonymous about 19 years ago

Legacy ID: #3109560 Legacy Poster: DD (ddevienne)

I've just switched from Xalan to SaxonB 8.4. I outputting indented XML, and specified a 2 char indent with saxon:indent-spaces="2" (I used a similar extension in Xalan). Previously with Xalan, <xsl:comment>s were indented the same way as XML elements, i.e. <bar /> <xsl:comment> foo </xsl:comment> <foo /> Would generate: <bar /> <!-- foo --> <foo /> Whereas now with Saxon it generates: <bar/><!-- foo --> <foo /> I tried using: <bar /> <xsl:text>&#xa;</xsl:text> <xsl:comment> foo </xsl:comment> <xsl:text>&#xa;</xsl:text> <foo /> But it's both ugly and still doesn't indent the comment as the surounding nodes. I just achieved putting the comment on it's own line. Can someone tell me how to achieve the desired commented indented as other nodes? Thanks, --DD PS: I don't know who's right. I'd assume Saxon, but all I want is a 'properly' formated XML output doc.


Replies (2)

RE: &lt;xsl:comment&gt; not indenting? - Added by Anonymous about 19 years ago

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

Yes, Saxon makes no attempt to indent comments. The spec places some constraints on what a serializer can do when indent="yes" is requested (for example, it can't add whitespace other than before a start tag or after an end tag; but the spec isn't prescriptive and there's certainly no conformance issue here. There are some circumstances where it would be wrong to indent comments (e.g. when they appear in the middle of text) but there are probably some cases where it's reasonably safe to do so. I'll bear it in mind as a future "to do" item, but my priority at the moment is on things that are needed for spec conformance. Michael Kay

RE: &lt;xsl:comment&gt; not indenting? - Added by Anonymous about 19 years ago

Legacy ID: #3110008 Legacy Poster: DD (ddevienne)

Thanks for your answer Master Kay, and thank you for considering extensions to ease indenting comments for usage such as mine that is considering it. For reference, I'm posting here the diff between the original XSL 1.0/Xalan stylesheet, and the XSL 2.0/Saxon one. You can see that achieving correct (ad-hoc) indentation of comments adds too many lines and is very ugly. Of course, I'm ignorant of XSL 2.0 right now, so there may be a much more elegant way to achieve the same effect (or even in plain XSL 1.0 for that matter ;-): FWIW, the other sticky point I found converting from Xalan's <redirect:write> to <xsl:result-document> is related to going from file="" to href="". One cannot simply prepend file: or file:/ to the absolute path. One must use the former on Unix and the latter on Windows. Saxon (or Java?) complains about an authority component in the URL otherwise. This is inconvenient. I deal with it with a conditional in Ant when passing the output.href param to the stylesheet, but this little issue is annoying for X-platform stylesheets. Again, thanks for your answer. I look forward to discovering the fine new XSL 2.0 features implemented by Saxon now that I have moved away from Xalan. Cheers from Houston, TX, --DD P:\com_lgc\cycle9\buildtools\cruisegen>cvs diff -r 1.1.2.22 -r 1.1.2.23 ccbuilds2cruise.xsl Index: ccbuilds2cruise.xsl =================================================================== RCS file: /cm3/cvs/buildtools/buildtools/cruisegen/Attic/ccbuilds2cruise.xsl,v retrieving revision 1.1.2.22 retrieving revision 1.1.2.23 diff -r1.1.2.22 -r1.1.2.23 7,8c7,8 < <xsl:stylesheet version="1.0" < xmlns:xalan="http://xml.apache.org/xslt" --- > <xsl:stylesheet version="2.0" > xmlns:saxon="http://saxon.sf.net/" 10d9 < xmlns:redirect="http://xml.apache.org/xalan/redirect" 12,13c11 < exclude-result-prefixes="xalan, h" < extension-element-prefixes="redirect"> --- > exclude-result-prefixes="h saxon"> 18c16 < xalan:indent-amount="2" /> --- > saxon:indent-spaces="2" /> 22c20,21 < <xsl:param name="output.dir" select="'.'" /> --- > <xsl:param name="output.dir" /> > <xsl:param name="output.href" /> 74c73 < <redirect:write file="{$output.dir}/{$ccname}/cruise.xml"> --- > <xsl:result-document href="{$output.href}/{$ccname}/cruise.xml"> 227c226,228 < <xsl:comment> CM non-standard user names </xsl:comment> --- > <xsl:call-template name="comment"><xsl:with-param name="string" > > CM non-standard user names </xsl:with-param> > </xsl:call-template> 242c243,246 < <xsl:comment> Users' non-standard user names </xsl:comment> --- > <xsl:call-template name="comment"><xsl:with-param name="string" > > Users' non-standard user names </xsl:with-param> > </xsl:call-template> > 247c251,254 < <xsl:comment> Always notify this project's contacts </xsl:comment> --- > <xsl:call-template name="comment"><xsl:with-param name="string" > > Always notify this project's contacts </xsl:with-param> > </xsl:call-template> > 252c259,262 < <xsl:comment> Always notify dscmdev mailing list </xsl:comment> --- > <xsl:call-template name="comment"><xsl:with-param name="string" > > Always notify dscmdev mailing list </xsl:with-param> > </xsl:call-template> > 260c270 < </redirect:write> --- > </xsl:result-document> 309a320,331 > </xsl:template> > > <!-- ================================================= > Saxon does not indent comment as Xalan used to do. > Circumvent this different using this ugly HACK... > --> > <xsl:template name="comment"> > <xsl:param name="string" /> > > <xsl:text>&#xa; </xsl:text> > <xsl:comment><xsl:value-of select="$string" /></xsl:comment> > <xsl:text>&#xa;</xsl:text>

    (1-2/2)

    Please register to reply