Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>With XSLT 2.0 (as implemented by Saxon 9 or AltovaXML tools) you can use xsl:for-each-group group-starting-with and a recursive function:</p> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:mf="http://example.com/2010/mf" exclude-result-prefixes="xsd mf"&gt; &lt;xsl:output indent="yes"/&gt; &lt;xsl:function name="mf:nest" as="element()*"&gt; &lt;xsl:param name="elements" as="element()*"/&gt; &lt;xsl:param name="level" as="xsd:integer"/&gt; &lt;xsl:for-each-group select="$elements" group-starting-with="*[starts-with(local-name(), concat('TITEL', $level))]"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="self::*[starts-with(local-name(), concat('TITEL', $level))]"&gt; &lt;xsl:element name="TITEL{$level}"&gt; &lt;xsl:apply-templates select="."/&gt; &lt;xsl:sequence select="mf:nest(current-group() except ., $level + 1)"/&gt; &lt;/xsl:element&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:apply-templates select="current-group()"/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:for-each-group&gt; &lt;/xsl:function&gt; &lt;xsl:template match="ROOT"&gt; &lt;xsl:sequence select="mf:nest(*, 1)"/&gt; &lt;/xsl:template&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="*[starts-with(local-name(), 'TITEL')]"&gt; &lt;TITEL&gt; &lt;xsl:apply-templates select="@* | node()"/&gt; &lt;/TITEL&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>With that stylesheet the input</p> <pre><code>&lt;ROOT&gt; &lt;TITEL1&gt;Titel 1, 1&lt;/TITEL1&gt; &lt;p&gt;..&lt;/p&gt; &lt;TITEL2&gt;Titel 2, 1&lt;/TITEL2&gt; &lt;TITEL3&gt;Titel 3, 1&lt;/TITEL3&gt; &lt;TITEL3&gt;Titel 3, 2&lt;/TITEL3&gt; &lt;P&gt;...&lt;/P&gt; &lt;/ROOT&gt; </code></pre> <p>is transformed to the output</p> <pre><code>&lt;TITEL1&gt; &lt;TITEL&gt;Titel 1, 1&lt;/TITEL&gt; &lt;p&gt;..&lt;/p&gt; &lt;TITEL2&gt; &lt;TITEL&gt;Titel 2, 1&lt;/TITEL&gt; &lt;TITEL3&gt; &lt;TITEL&gt;Titel 3, 1&lt;/TITEL&gt; &lt;/TITEL3&gt; &lt;TITEL3&gt; &lt;TITEL&gt;Titel 3, 2&lt;/TITEL&gt; &lt;P&gt;...&lt;/P&gt; &lt;/TITEL3&gt; &lt;/TITEL2&gt; &lt;/TITEL1&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