XML/XSLT stylesheet/comment

Материал из Web эксперт
Перейти к: навигация, поиск

Add comments to generated xml

File: Data.xml
<documentation>test</documentation>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="poem">
    <html>
      <xsl:comment>
        Created by release 3
      </xsl:comment>
      <xsl:apply-templates />
    </html>
  </xsl:template>
  
</xsl:stylesheet>
Output:
test



Copy comment with comment() function

File: Data.xml
<documentation>The following is a revision.</documentation>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  <xsl:template match="comment()">
    <xsl:comment>
      <xsl:value-of select="." />
    </xsl:comment>
  </xsl:template>
  
</xsl:stylesheet>
Output:
The following is a revision.



Create comment in style sheet

File: Data.xml
<documentation>The following is a revision.</documentation>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="documentation">
    <xsl:comment>
      <xsl:apply-templates />
    </xsl:comment>
  </xsl:template>
  
</xsl:stylesheet>
Output:
<!--The following is a revision.-->



Hyphens in xsl:comment element make it illegal

File: Data.xml
<documentation>The following is a revision.</documentation>
File: Transform.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="poem">
    <html>
      <xsl:comment>
        this is the comment
      </xsl:comment>
      <xsl:apply-templates />
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>The following is a revision.



match comment

File: Data.xml
<verse>test</verse>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output omit-xml-declaration="yes" />
  <xsl:template match="comment()">
    <doc>
      <xsl:value-of select="." />
    </doc>
  </xsl:template>
  <xsl:template match="verse">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>
</xsl:stylesheet>

Output:
<p>test</p>