Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It seems I answer my question myself. Here is the solution. Keep in mind that the level difference between two lists will be maximum +-1.</p> <pre class="lang-xml prettyprint-override"><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;!-- copy all other nodes --&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;!-- find every list element which has a preceding non-list element --&gt; &lt;xsl:template match="list[not(preceding-sibling::*[1][self::list])]"&gt; &lt;!-- now walk recursive through all lists --&gt; &lt;xsl:apply-templates select="self::list" mode="recurse"&gt; &lt;xsl:with-param name="level1_margin" select="@margin"/&gt; &lt;xsl:with-param name="level" select="1"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:template&gt; &lt;!-- remove other list elements, because they are recursive processed --&gt; &lt;xsl:template match="list"/&gt; &lt;!-- remove @margin from list --&gt; &lt;xsl:template match="list/@margin"/&gt; &lt;!-- go recursive through all following lists --&gt; &lt;xsl:template match="list" mode="recurse"&gt; &lt;xsl:param name="level1_margin" select="0"/&gt; &lt;xsl:param name="level" select="1"/&gt; &lt;xsl:variable name="nextStep" select="self::list/following-sibling::*[1][self::list]"/&gt; &lt;!-- create current list element with its level --&gt; &lt;xsl:apply-templates select="self::list" mode="create"&gt; &lt;xsl:with-param name="level" select="$level"/&gt; &lt;/xsl:apply-templates&gt; &lt;xsl:if test="$nextStep"&gt; &lt;xsl:choose&gt; &lt;!-- new start margin/point for level 1 --&gt; &lt;xsl:when test="($nextStep/@margin &amp;lt;= $level1_margin) or ($nextStep/@margin &amp;lt; @margin and $level = 2)"&gt; &lt;xsl:apply-templates select="$nextStep" mode="recurse"&gt; &lt;xsl:with-param name="level1_margin" select="$nextStep/@margin"/&gt; &lt;xsl:with-param name="level" select="1"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:when&gt; &lt;!-- -1 --&gt; &lt;xsl:when test="$nextStep/@margin &amp;lt; @margin and $level &amp;gt; 1"&gt; &lt;xsl:apply-templates select="$nextStep" mode="recurse"&gt; &lt;xsl:with-param name="level1_margin" select="$level1_margin"/&gt; &lt;xsl:with-param name="level" select="$level - 1"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:when&gt; &lt;!-- +1 --&gt; &lt;xsl:when test="$nextStep/@margin &amp;gt; @margin"&gt; &lt;xsl:apply-templates select="$nextStep" mode="recurse"&gt; &lt;xsl:with-param name="level1_margin" select="$level1_margin"/&gt; &lt;xsl:with-param name="level" select="$level + 1"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:when&gt; &lt;!-- +-0 --&gt; &lt;xsl:otherwise&gt; &lt;xsl:apply-templates select="$nextStep" mode="recurse"&gt; &lt;xsl:with-param name="level1_margin" select="$level1_margin"/&gt; &lt;xsl:with-param name="level" select="$level"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;!-- create list element with level attribute --&gt; &lt;xsl:template match="list" mode="create"&gt; &lt;xsl:param name="level"/&gt; &lt;list&gt; &lt;xsl:attribute name="level"&gt; &lt;xsl:value-of select="$level"/&gt; &lt;/xsl:attribute&gt; &lt;xsl:apply-templates select="@*"/&gt; &lt;xsl:apply-templates/&gt; &lt;/list&gt; &lt;/xsl:template&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