Note that there are some explanatory texts on larger screens.

plurals
  1. POComment a single node
    text
    copied!<p>Using XSLT, how can I comment a single node without commenting its children?</p> <p>I have this html:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;div class="blah" style="blahblah"&gt; &lt;span&gt; &lt;p&gt;test&lt;/p&gt; &lt;/span&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I would like this output:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;!-- div class="blah" style="blahblah" --&gt; &lt;span&gt; &lt;p&gt;test&lt;/p&gt; &lt;/span&gt; &lt;!-- /div --&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>It is key that the children nodes are copied and any attributes of the commented node are also copied.</p> <p>The following is my best try but doesn't work. XSLT processor yells out: "Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added."</p> <pre><code>&lt;?xml version="1.0" ?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;!-- IdentityTransform --&gt; &lt;xsl:template match="/ | @* | node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@* | node()" /&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="div"&gt; &lt;xsl:text disable-output-escaping="yes"&gt;&amp;lt;!--&lt;/xsl:text&gt; &lt;xsl:copy&gt; &lt;xsl:text disable-output-escaping="yes"&gt;--&amp;gt;&lt;/xsl:text&gt; &lt;xsl:apply-templates select="@* | node()"/&gt; &lt;xsl:text disable-output-escaping="yes"&gt;&amp;lt;!--&lt;/xsl:text&gt; &lt;/xsl:copy&gt; &lt;xsl:text disable-output-escaping="yes"&gt;--&amp;gt;&lt;/xsl:text&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload