Comments

1. Keeping comments in the output
2. Comments in HTML
3. Adding comments to the output
4. How to extract comments only
5. Adding comments to a stylesheet

1.

Keeping comments in the output

Ken Holman




> I wonder  if there is a better way to keep the
>comments from my source document

an example is below that preserves comments and text nodes but nothing else.

T:\ftemp>type julie.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<doc>
   <test>
     <!--first comment-->
   </test>
   <!--second comment-->
</doc>

T:\ftemp>type julie.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">

<xsl:template match="comment()">
   <xsl:copy/>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>saxon julie.xml julie.xsl
<?xml version="1.0" encoding="utf-8"?>

     <!--first comment-->

   <!--second comment-->

2.

Comments in HTML

DaveP

How to output comments in HTML

Try <xsl:comment> Comment content </xsl:comment>

3.

Adding comments to the output

J.Pietschmann



> If I could get comments in the style sheet to be passed to the output then
> placing the Java script inside the comment would give the desired result.
> e.g.
>
> <!--
>
> java code
> blah blah
>
> -->
>
> Unfortunately, comments in the style sheet seem to be ignored.

Comments in the style sheet are comments in the style sheet: they presumably comment on the stlye sheet, so you don't want to have them in the output. If you want to put a comment into the transformation result, use xsl:comment.

4.

How to extract comments only

Mike Brown


> I'm still new to XSLT, but have a need to
> extract all comments (however deep) from an
> XML file to another. Output should only
> contain comments and nothing else perhaps
> save the XML declaration (i.e: none
> of the other markup/pcdata).

Sure. Read http://www.w3.org/TR/xslt#built-in-rule and then it should be apparent why this simple stylesheet will (well, should) do what you want:

<?xml version="1.0"?>
<xsl:stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="comment()">
  <xsl:copy/>
</xsl:template>

<xsl:template match="text()|@*"/>

</xsl:stylesheet>

Nick Browne adds

Try :

<xsl:template match="/">
  <xsl:for-each select="//comment()">
   <SRC_COMMENT>
   <xsl:value-of select="."/>
   </SRC_COMMENT>
  </xsl:for-each>
 </xsl:template>

or use a <xsl:comment ...> instruction for a more literal duplication of the source document content in place of my <SRC_COMMENT> tag.

5.

Adding comments to a stylesheet

Mike Kay



 > Is it  possible to utilise a given namespace for
 > (effectively) comments, i.e. the named namespace is not
 > treated as a literal and output.
 > 
 > Rationale:
 > 
 >  E.g. constructing an
 > outline of the stylesheet operation,
 > explanatory documentation,  
 > providing multi-modal representations, etc.
 > 
 > The stylesheet application then simply bypasses anything
 > in that namespace, treating it as a comment.
 > 
 > 
 > This would support conformance to the XML accessibility guidelines.

There is a neat trick you can use for this: just declare your namespace as an extension namespace.

 <xsl:stylesheet
    xmlns:x="my.accessibility.namespace"
    extension-element-prefixes="x">
 
 
    <x:comment>....</x:comment>

The XSLT processor is required to ignore extension instructions in a namespace that it does not recognize.

David C finishes this off with

Oh. I use this trick a lot but I always throw in an empty xsl:fallback element as well I thought this was needed and was surprised by your comment

The XSLT processor is required to ignore extension instructions in a namespace that it does not recognize.

the XSLT 1.0 spec seems a bit obscure here.

14.1 says An XSLT processor must not signal an error merely because a template contains an extension element for which no implementation is available.

However the preceding sentence is

When such an extension element is instantiated, then the XSLT processor must perform fallback for the element as specified in [15 Fallback].

and fallback says:

if the instruction element has one or more xsl:fallback children, then the content of each of the xsl:fallback children must be instantiated in sequence; otherwise, an error must be signaled.

I have always read this as saying if there are no xsl;fallback elements then the "otherwise" clause implies that an error will be signaled.

(Mike later replied, You're right. There must be an <xsl:fallback/>. I was thinking of top-level elements.