Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This transformation</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="kListGroup" match="list" use="generate-id( preceding-sibling::node()[not(self::list)][1] )"/&gt; &lt;xsl:template match="node()|@*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()[1]|@*"/&gt; &lt;/xsl:copy&gt; &lt;xsl:apply-templates select= "following-sibling::node()[1]"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match= "list[preceding-sibling::node()[1][not(self::list)]]"&gt; &lt;ol&gt; &lt;xsl:apply-templates mode="listgroup" select= "key('kListGroup', generate-id(preceding-sibling::node()[1]) ) [not(@level) or @level = 1] "/&gt; &lt;/ol&gt; &lt;xsl:apply-templates select= "following-sibling::node()[not(self::list)][1]"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="list" mode="listgroup"&gt; &lt;li&gt; &lt;xsl:value-of select="."/&gt; &lt;xsl:variable name="vNext" select= "following-sibling::list [not(@level &gt; current()/@level)][1] | following-sibling::node()[not(self::list)][1] "/&gt; &lt;xsl:variable name="vNextLevel" select= "following-sibling::list [@level = current()/@level +1] [generate-id(following-sibling::list [not(@level &gt; current()/@level)][1] | following-sibling::node()[not(self::list)][1] ) = generate-id($vNext) ] "/&gt; &lt;xsl:if test="$vNextLevel"&gt; &lt;ol&gt; &lt;xsl:apply-templates mode="listgroup" select="$vNextLevel"/&gt; &lt;/ol&gt; &lt;/xsl:if&gt; &lt;/li&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on this XML document (intentionally complicated to show that the solution works in many edge cases)</strong>:</p> <pre><code>&lt;root&gt; &lt;h1&gt;text&lt;/h1&gt; &lt;list level="1"&gt;1.1&lt;/list&gt; &lt;list level="1"&gt;1.2&lt;/list&gt; &lt;list level="2"&gt;1.2.1&lt;/list&gt; &lt;list level="2"&gt;1.2.2&lt;/list&gt; &lt;list level="3"&gt;1.2.2.1&lt;/list&gt; &lt;list level="1"&gt;1.3&lt;/list&gt; &lt;p&gt;text&lt;/p&gt; &lt;list&gt;2.1&lt;/list&gt; &lt;list&gt;2.2&lt;/list&gt; &lt;h2&gt;text&lt;/h2&gt; &lt;h1&gt;text&lt;/h1&gt; &lt;list level="1"&gt;3.1&lt;/list&gt; &lt;list level="1"&gt;3.2&lt;/list&gt; &lt;list level="2"&gt;3.2.1&lt;/list&gt; &lt;list level="2"&gt;3.2.2&lt;/list&gt; &lt;list level="3"&gt;3.2.2.1&lt;/list&gt; &lt;list level="1"&gt;3.3&lt;/list&gt; &lt;list level="2"&gt;3.3.1&lt;/list&gt; &lt;list level="2"&gt;3.3.2&lt;/list&gt; &lt;p&gt;text&lt;/p&gt; &lt;/root&gt; </code></pre> <p><strong>produces the wanted, correct result</strong>:</p> <pre><code>&lt;root&gt; &lt;h1&gt;text&lt;/h1&gt; &lt;ol&gt; &lt;li&gt;1.1&lt;/li&gt; &lt;li&gt;1.2&lt;ol&gt; &lt;li&gt;1.2.1&lt;/li&gt; &lt;li&gt;1.2.2&lt;ol&gt; &lt;li&gt;1.2.2.1&lt;/li&gt; &lt;/ol&gt; &lt;/li&gt; &lt;/ol&gt; &lt;/li&gt; &lt;li&gt;1.3&lt;/li&gt; &lt;/ol&gt; &lt;p&gt;text&lt;/p&gt; &lt;ol&gt; &lt;li&gt;2.1&lt;/li&gt; &lt;li&gt;2.2&lt;/li&gt; &lt;/ol&gt; &lt;h2&gt;text&lt;/h2&gt; &lt;h1&gt;text&lt;/h1&gt; &lt;ol&gt; &lt;li&gt;3.1&lt;/li&gt; &lt;li&gt;3.2&lt;ol&gt; &lt;li&gt;3.2.1&lt;/li&gt; &lt;li&gt;3.2.2&lt;ol&gt; &lt;li&gt;3.2.2.1&lt;/li&gt; &lt;/ol&gt; &lt;/li&gt; &lt;/ol&gt; &lt;/li&gt; &lt;li&gt;3.3&lt;ol&gt; &lt;li&gt;3.3.1&lt;/li&gt; &lt;li&gt;3.3.2&lt;/li&gt; &lt;/ol&gt; &lt;/li&gt; &lt;/ol&gt; &lt;p&gt;text&lt;/p&gt; &lt;/root&gt; </code></pre> <p><strong>or as displayed by the browser</strong>:</p> <p></p> <h1>text</h1> <ol> <li>1.1</li> <li>1.2<ol> <li>1.2.1</li> <li>1.2.2<ol> <li>1.2.2.1</li> </ol> </li> </ol> </li> <li>1.3</li> </ol> <p>text</p> <ol> <li>2.1</li> <li>2.2</li> </ol> <h2>text</h2> <h1>text</h1> <ol> <li>3.1</li> <li>3.2<ol> <li>3.2.1</li> <li>3.2.2<ol> <li>3.2.2.1</li> </ol> </li> </ol> </li> <li>3.3<ol> <li>3.3.1</li> <li>3.3.2</li> </ol> </li> </ol> <p>text</p> <p></p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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