Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You'll have to do an <code>xsl:apply-templates</code> for the preceding sibling <code>comment()</code>'s. Since you're changing the order of <code>weight</code>/<code>color</code>/<code>state</code>, you'll have to add it to each one of their templates.</p> <p>Also, matching <code>Comment 3</code> is tricky and what I did might not work for your actual data.</p> <p><strong>XML Input</strong></p> <pre><code>&lt;elements&gt; &lt;stack&gt; &lt;name&gt;Ray&lt;/name&gt; &lt;status&gt;0&lt;/status&gt; &lt;/stack&gt; &lt;!-- Comment 1 --&gt; &lt;things&gt; &lt;!-- Comment 2 --&gt; &lt;thing&gt; &lt;color&gt;red&lt;/color&gt; &lt;!-- State Comment --&gt; &lt;state&gt;solid&lt;/state&gt; &lt;!-- Weight Comment 1 --&gt; &lt;!-- Weight Comment 2 --&gt; &lt;weight&gt;45&lt;/weight&gt; &lt;unit&gt;34&lt;/unit&gt; &lt;!-- Comment 3 --&gt; &lt;/thing&gt; &lt;/things&gt; &lt;favs&gt; &lt;stick&gt;ready&lt;/stick&gt; &lt;!-- Comment 4--&gt; &lt;/favs&gt; &lt;/elements&gt; </code></pre> <p><strong>XSLT 1.0</strong></p> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&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="things"&gt; &lt;mainElements&gt; &lt;xsl:apply-templates select="thing|comment()"/&gt; &lt;/mainElements&gt; &lt;/xsl:template&gt; &lt;xsl:template match="thing"&gt; &lt;specialThing&gt; &lt;xsl:apply-templates select="weight"/&gt; &lt;xsl:apply-templates select="color"/&gt; &lt;xsl:apply-templates select="state"/&gt; &lt;/specialThing&gt; &lt;xsl:apply-templates select="comment()[not(following-sibling::*)]"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="weight"&gt; &lt;xsl:apply-templates select="preceding-sibling::comment()[generate-id(following-sibling::*[1])=generate-id(current())]"/&gt; &lt;PropertyOne&gt; &lt;xsl:value-of select="."/&gt; &lt;/PropertyOne&gt; &lt;/xsl:template&gt; &lt;xsl:template match="color"&gt; &lt;xsl:apply-templates select="preceding-sibling::comment()[generate-id(following-sibling::*[1])=generate-id(current())]"/&gt; &lt;PropertyTwo&gt; &lt;xsl:value-of select="."/&gt; &lt;/PropertyTwo&gt; &lt;/xsl:template&gt; &lt;xsl:template match="state"&gt; &lt;xsl:apply-templates select="preceding-sibling::comment()[generate-id(following-sibling::*[1])=generate-id(current())]"/&gt; &lt;PropertyThree&gt; &lt;xsl:value-of select="."/&gt; &lt;/PropertyThree&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>XML Output</strong></p> <pre><code>&lt;elements&gt; &lt;stack&gt; &lt;name&gt;Ray&lt;/name&gt; &lt;status&gt;0&lt;/status&gt; &lt;/stack&gt; &lt;!-- Comment 1 --&gt; &lt;mainElements&gt; &lt;!-- Comment 2 --&gt; &lt;specialThing&gt; &lt;!-- Weight Comment 1 --&gt; &lt;!-- Weight Comment 2 --&gt; &lt;PropertyOne&gt;45&lt;/PropertyOne&gt; &lt;PropertyTwo&gt;red&lt;/PropertyTwo&gt; &lt;!-- State Comment --&gt; &lt;PropertyThree&gt;solid&lt;/PropertyThree&gt; &lt;/specialThing&gt; &lt;!-- Comment 3 --&gt; &lt;/mainElements&gt; &lt;favs&gt; &lt;stick&gt;ready&lt;/stick&gt; &lt;!-- Comment 4--&gt; &lt;/favs&gt; &lt;/elements&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