Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Here is an efficient and short solution, using keys <a href="http://www.jenitennison.com/xslt/grouping/muenchian.html" rel="nofollow">(Muenchian grouping</a>)</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="kDocById" match="document" use="@refid"/&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="group"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select= "@* | node()[not(self::document)] | document [generate-id() = generate-id(key('kDocById', @refid)[1]) ]"/&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> (reflecting the latest refinement made in a comment by the OP):</p> <pre><code>&lt;data&gt; &lt;group id= "1"&gt; &lt;phrase&gt;Doc1&lt;/phrase&gt; &lt;document refid ="3"/&gt; &lt;document refid ="5"/&gt; &lt;document refid= "1"/&gt; &lt;/group&gt; &lt;group id= "2"&gt; &lt;phrase&gt;Doc2&lt;/phrase&gt; &lt;document refid ="2"/&gt; &lt;document refid ="3"/&gt; &lt;document refid= "6"/&gt; &lt;/group&gt; &lt;group id= "3"&gt; &lt;phrase&gt;Doc3&lt;/phrase&gt; &lt;document refid ="2"/&gt; &lt;document refid ="3"/&gt; &lt;document refid= "4"/&gt; &lt;/group&gt; &lt;/data&gt; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>&lt;data&gt; &lt;group id="1"&gt; &lt;phrase&gt;Doc1&lt;/phrase&gt; &lt;document refid="3"/&gt; &lt;document refid="5"/&gt; &lt;document refid="1"/&gt; &lt;/group&gt; &lt;group id="2"&gt; &lt;phrase&gt;Doc2&lt;/phrase&gt; &lt;document refid="2"/&gt; &lt;document refid="6"/&gt; &lt;/group&gt; &lt;group id="3"&gt; &lt;phrase&gt;Doc3&lt;/phrase&gt; &lt;document refid="4"/&gt; &lt;/group&gt; &lt;/data&gt; </code></pre>
    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