Re: [xsl] How to get comments to indent on their own line in XML output?

Subject: Re: [xsl] How to get comments to indent on their own line in XML output?
From: alan.painter@xxxxxxx
Date: Sun, 6 Jul 2008 09:33:23 +0200
Here's an example of what I'm trying to do.  (Shoulda posted this originally,
sorry.)

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0" >
<xsl:output indent="yes" encoding="UTF-8" method="xml" />

<xsl:template match="/" name="main" >

<root rootAttribute="example">
<xsl:comment select="'Here is my comment.'" />
<property example1="example1" />
<property example2="example2" />
</root>

</xsl:template>

</xsl:stylesheet>

I'd like this to produce:

<?xml version="1.0" encoding="UTF-8"?>
<root rootAttribute="example">
<!--Here is my comment.-->
<property example1="example1"/>
<property example2="example2"/>
</root>

but instead I get serialized indented output with the "root" and first
"property" elements appearing, with the comment between them, all on
the same line, like this.

<?xml version="1.0" encoding="UTF-8"?>
<root rootAttribute="example"><!--Here is my comment.--><property
example1="example1"/>
<property example2="example2"/>
</root>

I'm trying to get some folks to adopt XSLT for generating
human-readable configuration files for another application and the
comments tend to be important.

thanks again

-alan



----- Original Message -----
From: "Michael Kay" [mike@xxxxxxxxxxxx]
Sent: 04/07/2008 23:10 CET
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE: [xsl] How to get comments to indent on their own line in XML
output?




The serialization spec (for 2.0) says that when indenting, whitespace may be
added only immediately before an (element) start tag or immediately after an
end tag. This means it can't be inserted between a comment and a text node.
Although there's no such rule in XSLT 1.0, the logic is the same; it's
generally unsafe. For example:

<date-of-birth>1832-01<!--I think it's a 1 but it might be a
7-->-05</date-of-birth>

If you add whitespace around the comment, the document becomes invalid
against the schema.

One could do better in some cases, for example for a comment that
immediately follows an end tag (or is separated from it only by whitespace);
but in the end, you have to decide how much effort to put into fine-tuning
the algorithm.

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: alan.painter@xxxxxxx [mailto:alan.painter@xxxxxxx]
> Sent: 04 July 2008 19:20
> To: xsl-list
> Subject: [xsl] How to get comments to indent on their own
> line in XML output?
>
>
>
>
> Hi,
>
> I'm producing some XML output with indented="yes" and the
> indenting works great excelpt for the comment nodes, which
> push the following elements to the end of the comment line.
>
> I would have expected a comment to occupy its own line
> starting at current indentation level, with the following
> element on next line at same indentation.
>
> I've tried a few naC/ve attempts at adding text nodes with
> newlines after comments, but no dice.
>
> Any suggestions for this?
>
> Thanks
>
> -alan
>
>
> Ensemble adoptons des gestes responsables : N'imprimez ce
> mail que si necessaire.
>
> Les informations contenues dans ce message et les pieces
> jointes (ci-apres denomme le message) sont confidentielles et
> peuvent etre couvertes par le secret professionnel. Si vous
> n'etes pas le destinataire de ce message, il vous est
> interdit de le copier, de le faire suivre, de le divulguer ou
> d'en utiliser tout ou partie. Si vous avez recu ce message
> par erreur, nous vous remercions de le supprimer de votre
> systeme, ainsi que toutes ses copies, et d'en avertir
> immediatement HSBC France et ses filiales par message de
> retour. Il est impossible de garantir que les communications
> par messagerie electronique arrivent en temps utile, sont
> securisees ou denuees de toute erreur, alteration,
> falsification ou virus. En consequence, HSBC France et ses
> filiales declinent toute responsabilite du fait des erreurs,
> alterations, falsifications ou omissions qui pourraient en resulter.
>
> Consider the environment before printing this mail.
>
> The information contained in this e-mail is confidential. It
> may also be legally privileged. If you are not the addressee
> you may not copy, forward, disclose or use any part of it. If
> you have received this message by error, please delete it and
> all copies from your system and notify the sender immediately
> by return e-mail. E-mail communications cannot be guaranteed
> to be timely secure, error or virus-free. The sender does not
> accept liability for any errors or omissions which arise as a result.


****************************************************************
Ce message a ete transmis par Internet. Son emetteur peut ne pas etre
l'emetteur annonce. Son contenu et toute piece jointe peuvent ne pas etre
exacts.
This message originated from the Internet. Its originator may or may not be
who they claim to be and information contained in the message and any
attachments may or may not be accurate.


Ensemble adoptons des gestes responsables : N'imprimez ce mail que si
necessaire.

Les informations contenues dans ce message et les pieces jointes (ci-apres
denomme le message) sont confidentielles et peuvent etre couvertes par le
secret professionnel. Si vous n'etes pas le destinataire de ce message, il
vous est interdit de le copier, de le faire suivre, de le divulguer ou d'en
utiliser tout ou partie. Si vous avez recu ce message par erreur, nous vous
remercions de le supprimer de votre systeme, ainsi que toutes ses copies, et
d'en avertir immediatement HSBC France et ses filiales par message de retour.
Il est impossible de garantir que les communications par messagerie
electronique arrivent en temps utile, sont securisees ou denuees de toute
erreur, alteration, falsification ou virus. En consequence, HSBC France et ses
filiales declinent toute responsabilite du fait des erreurs, alterations,
falsifications ou omissions qui pourraient en resulter.

Consider the environment before printing this mail.

The information contained in this e-mail is confidential. It may also be
legally privileged. If you are not the addressee you may not copy, forward,
disclose or use any part of it. If you have received this message by error,
please delete it and all copies from your system and notify the sender
immediately by return e-mail. E-mail communications cannot be guaranteed to be
timely secure, error or virus-free. The sender does not accept liability for
any errors or omissions which arise as a result.

Current Thread