2

I have several XSLT code files and I want to modify them to add some comments after functions or explaining them etc. So let's imagine I have 10 XSLT files and I want to transform them with additional comments for each function. The code will not change at all, I just want to add automatic documentation – that's all.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Sojimanatsu
  • 619
  • 11
  • 28
  • Thanks ! i could not find any sample examples of this, or kinda some discussions. Thats why I asked it in here – Sojimanatsu Jul 13 '18 at 12:30
  • See http://www.balisage.net/Proceedings/vol8/print/Clark01/BalisageVol8-Clark01.html for another use case for meta-stylesheets. – Michael Kay Jul 13 '18 at 22:13

2 Answers2

2

Absolutely. Meta XSLT transformations are wonderfully powerful.

Just adding comments is a relatively simple matter that can be performed with the identity transformation plus some specialized templates that insert comments before, after, or around a site of your choosing.

For example:

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

  <!-- identity transformation -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- override for inserting a comment -->    
  <xsl:template match="div[@id='target']">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <xsl:comment>The above div element is comment-worthy.</xsl:comment>
  </xsl:template>
</xsl:stylesheet>

More complicated meta XSLT transformations are certainly possible too:

kjhughes
  • 106,133
  • 27
  • 181
  • 240
1

XSLT is XML so you can use it as the input and you can as well produce it as the output of an XSLT transformation. Creating new XSLT elements as a result might require the use of https://www.w3.org/TR/xslt-30/#element-namespace-alias but inserting comments can be done with xsl:comment.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110