Re: [xsl] Best way of preserving comments?

Subject: Re: [xsl] Best way of preserving comments?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 03 Apr 2013 14:23:46 +0100
On 03/04/2013 14:08, Wolfgang Laun wrote:
Using XSLT 2, this combination:

  <xsl:strip-space elements = "*" />
  <xsl:template match="element()|attribute()">... skip selected *|@* ... />
  <xsl:template match="text()|@_ix">
    <xsl:copy-of select="."/>
  </xsl:template>
  <xsl:template match="comment()">
    <xsl:copy-of select="."/><xsl:text>
</xsl:text>
  </xsl:template>

is the best I could do to preserve the original comment and element
layout, shown with beginning of input and output XML, below. Can this
be improved? Losing empty lines is tolerable, losing line breaks is
not so great.

Thanks
Wolfgang

Within the body of the document just copying comments is fine but before or after the document element white space nodes are not generated as the default XDM generation does not consider white space children of the root node to be significant (and of course there are no other text node children of the root node in a well formed document.


So what I do is have two templates

<xsl:template match="/comment()" priority="2">
   <xsl:copy-of select="."/><xsl:text>&#10;</xsl:text>
<xsl:text>


<xsl:template match="comment()"> <xsl:copy-of select="."/> <xsl:text>

Of course this doesn't preserve newlines around initial comments
it just adds a newline whether there was one there or not but it works OK in practice.


Sometimes I also have

<xsl:template match="/" priority="2">
  <xsl:text>&#10;</xsl:text>
  <xsl:apply-templates/>
<xsl:text>

as well to get a newline at the start


David



________________________________________________________________________ The Numerical Algorithms Group Ltd is a company registered in England and Wales with company number 1249803. The registered office is: Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. ________________________________________________________________________


Current Thread