tags:

views:

28

answers:

1

Simple question. I have an XML file with a few dozens of comment blocks. This is being transformed by a stylesheet to generate an HTML page. However, the comments are ignored with this. But at the bottom of the generated HTML I would like to have a list of all comments within the XML file. Is that even possible, without the use of anything else than pure XSLT? (No Javascript or whatever!)


As far as I know, this is not possible, but I could be wrong...

+5  A: 

The reason the comments aren't processed is that the default template for comments do nothing:

<xsl:template match="processing-instruction()|comment()"/>

See XSLT 1.0 spec "Built-in Template Rules".

If you would like to do something else with comments, you could just create your own matching template and output them as either a new XML comment using xsl:comment or make a HTML list:

<xsl:template match="/">
  <ul>
    <xsl:apply-templates select="//comment()"/>
  </ul>
</xsl:template>

<xsl:template match="comment()">
  <li>
    <xsl:value-of select="."/>
  </li>
</xsl:template>
Per T
I was just about to give the same answer.... You might want to mention though you can use the **xsl:comment** element (rather than **li** in your example), if you still wanted to output the original XML comment as a comment in the HTML.
Tim C
@Tim C: Read my answer again, I think you missed a line! ;)
Per T
D'oh! My bad! If I could downvote my comment, I would!
Tim C
+1 for a correct answer.
Dimitre Novatchev
+1 Good answer.
Alejandro
Very good answer, even! :-)
Workshop Alex