Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Some answers to this question are good but rather inefficient (O(N^2)).</strong> </p> <p>This is so, because the path is constructed from scratch for every <code>Positionen</code> element. The average path length is N/2 and there are N <code>Positionen</code> elements. That means that N*N/2 operations are needed as minimum to construct all paths -- and this is quadratical time complexity.</p> <p><strong>Here is a more efficient O(N*log(N)) -- could be even (O(N) -- linear) solution in case it is acceptable for the <code>Positionen</code> elements in the output to be unsorted</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:key name="kChildren" match="Positionen" use="Parent"/&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="/"&gt; &lt;xsl:variable name="vrtfPass1"&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:variable&gt; &lt;xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/&gt; &lt;xsl:apply-templates select="$vPass1/*" mode="pass2"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="Positionen[not(number(Parent))]" mode="path"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Positionen" mode="path"&gt; &lt;xsl:param name="pPath"/&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;Path&gt;&lt;xsl:value-of select="concat($pPath, ID)"/&gt;&lt;/Path&gt; &lt;/xsl:copy&gt; &lt;xsl:apply-templates select="key('kChildren', ID)" mode="path"&gt; &lt;xsl:with-param name="pPath" select="concat($pPath, ID, '/')"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/*" mode="pass2"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"&gt; &lt;xsl:sort select="ID" data-type="number"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:copy&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;Positions&gt; &lt;Positionen&gt; &lt;ID&gt;1&lt;/ID&gt; &lt;Parent&gt;&lt;/Parent&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;2&lt;/ID&gt; &lt;Parent&gt;1&lt;/Parent&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;3&lt;/ID&gt; &lt;Parent&gt;1&lt;/Parent&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;4&lt;/ID&gt; &lt;Parent&gt;2&lt;/Parent&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;5&lt;/ID&gt; &lt;Parent&gt;4&lt;/Parent&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;6&lt;/ID&gt; &lt;Parent&gt;2&lt;/Parent&gt; &lt;/Positionen&gt; &lt;/Positions&gt; </code></pre> <p><strong>the wanted, correct result is produced:</strong></p> <pre><code>&lt;Positions&gt; &lt;Positionen&gt; &lt;ID&gt;1&lt;/ID&gt; &lt;Parent/&gt; &lt;Path&gt;1&lt;/Path&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;2&lt;/ID&gt; &lt;Parent&gt;1&lt;/Parent&gt; &lt;Path&gt;1/2&lt;/Path&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;3&lt;/ID&gt; &lt;Parent&gt;1&lt;/Parent&gt; &lt;Path&gt;1/3&lt;/Path&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;4&lt;/ID&gt; &lt;Parent&gt;2&lt;/Parent&gt; &lt;Path&gt;1/2/4&lt;/Path&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;5&lt;/ID&gt; &lt;Parent&gt;4&lt;/Parent&gt; &lt;Path&gt;1/2/4/5&lt;/Path&gt; &lt;/Positionen&gt; &lt;Positionen&gt; &lt;ID&gt;6&lt;/ID&gt; &lt;Parent&gt;2&lt;/Parent&gt; &lt;Path&gt;1/2/6&lt;/Path&gt; &lt;/Positionen&gt; &lt;/Positions&gt; </code></pre> <p><strong>Do Note</strong>:</p> <p>Every Path is produced by adding the current <code>ID</code> to the path of the parent (which is calculated only once) -- an O(1) operation. Fot the total of N paths this is O(N).</p> <p>The final sorting makes the time complexity O(N*log(N)) -- still much better than quadratical.</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.
    3. 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