Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select all comment nodes in an XML file?

Tags:

xslt

xslt-1.0

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...

like image 363
Wim ten Brink Avatar asked Oct 01 '10 07:10

Wim ten Brink


1 Answers

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>
like image 72
Per T Avatar answered Nov 15 '22 09:11

Per T