Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>I would do it this way</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:key name="kPreceding" match="para" use="generate-id(following-sibling::section[1])"/&gt; &lt;xsl:template match="node()|@*" name="identity"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="section[preceding-sibling::para]"&gt; &lt;comment&gt; &lt;xsl:apply-templates mode="copy" select="key('kPreceding', generate-id())"/&gt; &lt;/comment&gt; &lt;xsl:call-template name="identity"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match= "para[following-sibling::section]"/&gt; &lt;xsl:template match="para" mode="copy"&gt; &lt;xsl:call-template name="identity"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when this transformation is applied on the provided XML document:</strong></p> <pre><code>&lt;section&gt; &lt;title&gt;Main Section&lt;/title&gt; &lt;para&gt;Here is some text that is a child of a main section.&lt;/para&gt; &lt;para&gt;Some more text.&lt;/para&gt; &lt;para&gt;When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.&lt;/para&gt; &lt;section&gt; &lt;title&gt;This is my subsection&lt;/title&gt; &lt;para&gt;Text that is inside of the sub-section&lt;/para&gt; &lt;para&gt;And some more sub section text.&lt;/para&gt; &lt;/section&gt; &lt;/section&gt; </code></pre> <p><strong>the wanted, correct result is produced:</strong></p> <pre><code>&lt;section&gt; &lt;title&gt;Main Section&lt;/title&gt; &lt;comment&gt; &lt;para&gt;Here is some text that is a child of a main section.&lt;/para&gt; &lt;para&gt;Some more text.&lt;/para&gt; &lt;para&gt;When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.&lt;/para&gt; &lt;/comment&gt; &lt;section&gt; &lt;title&gt;This is my subsection&lt;/title&gt; &lt;para&gt;Text that is inside of the sub-section&lt;/para&gt; &lt;para&gt;And some more sub section text.&lt;/para&gt; &lt;/section&gt; &lt;/section&gt; </code></pre> <p><strong>Explanation</strong>:</p> <ol> <li><p>The identity rule (template) copies every node "as-is".</p></li> <li><p>The overriding template for <code>section</code> that has preceding sibling(s) <code>para</code> wraps all such siblings with a <code>comment</code> element, then calls the identity transformation on itself.</p></li> <li><p>For convenience we define a key that matches all <code>para</code> elements preceding a <code>section</code> element with a given <code>generate-id()</code>.</p></li> <li><p>The <code>para</code> elements that have a following sibling <code>section</code> are excluded from the action of the identity rule by an overriding template, that simply does nothing.</p></li> <li><p>Finally, such <code>para</code> elements, when being output within the wrapper <code>comment</code> are processed in mode <code>copy</code>, which simply calls the identity rule to do the copying.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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