How to select all comment nodes in an XML file?

Simple question. I have an XML file with dozens of comment blocks. This is converted by a stylesheet to create an HTML page. However, comments are ignored. But at the bottom of the generated HTML, I would like to have a list of all the comments in the XML file. Is this possible without using anything other than pure XSLT? (No javascript or anything else!)


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

+4
source share
1 answer

The reason comments are not processed is because the default template for comments does nothing:

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

See XSLT 1.0 Specification for Built-in Template Rules .

If you want to do something else with comments, you can simply create your own matching template and output them as a new XML comment using xsl:comment or create an 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> 
+10
source

All Articles