Note that there are some explanatory texts on larger screens.

plurals
  1. POXSLT grouping and leaving out duplicates
    text
    copied!<p>I have the following XML input file:</p> <pre><code>&lt;mappings&gt; &lt;mapping&gt; &lt;key&gt;6718&lt;/key&gt; &lt;value attribute="content_type"&gt;Info Page&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;35905&lt;/key&gt; &lt;value attribute="content_type"&gt;Press releases&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;6718&lt;/key&gt; &lt;value attribute="content_type"&gt;Info Page&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;36941&lt;/key&gt; &lt;value attribute="content_type"&gt;Press releases&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;24920&lt;/key&gt; &lt;value attribute="content_type"&gt;Press releases&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;40244&lt;/key&gt; &lt;value attribute="content_type"&gt;Press releases&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;36639&lt;/key&gt; &lt;value attribute="content_type"&gt;Press releases&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;1861&lt;/key&gt; &lt;value attribute="content_type"&gt;Press releases&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;2280&lt;/key&gt; &lt;value attribute="content_type"&gt;Press releases&lt;/value&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;key&gt;42062&lt;/key&gt; &lt;value attribute="content_type"&gt;Press releases&lt;/value&gt; &lt;/mapping&gt; &lt;/mappings&gt; </code></pre> <p>I would like to produce the following XML:</p> <pre><code>&lt;reductions&gt; &lt;reduction&gt; &lt;group&gt;Info Page&lt;/group&gt; &lt;key&gt;6718&lt;/key&gt; &lt;/reduction&gt; &lt;reduction&gt; &lt;group&gt;Press releases&lt;/group&gt; &lt;key&gt;35905&lt;/key&gt; &lt;key&gt;36941&lt;/key&gt; &lt;key&gt;24920&lt;/key&gt; &lt;key&gt;40244&lt;/key&gt; &lt;key&gt;36639&lt;/key&gt; &lt;key&gt;1861&lt;/key&gt; &lt;key&gt;2280&lt;/key&gt; &lt;key&gt;42062&lt;/key&gt; &lt;/reduction&gt; &lt;/reductions&gt; </code></pre> <p>So the keys from the input XML need to be grouped in the output XML based on the value of the value node from the input XML. Also the duplicates need to be removed, the input XML contans the key with value 6718 twice, the output XML only once. <br/><br/>I'm using XSLT 2.0 with Saxon 9 HE.<br/><br/>Can someone please help me?<br/><br/>[EDIT]</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"&gt; &lt;xsl:template match="/"&gt; &lt;xsl:variable name="mappings"&gt; &lt;mappings&gt; &lt;xsl:apply-templates select="resource/R"/&gt; &lt;/mappings&gt; &lt;/xsl:variable&gt; &lt;reductions&gt; &lt;xsl:apply-templates select="$mappings/*"/&gt; &lt;/reductions&gt; &lt;/xsl:template&gt; &lt;xsl:template match="R"&gt; &lt;mapping&gt; &lt;key&gt;&lt;xsl:value-of select="MT[@N='content_id']/@V"/&gt;&lt;/key&gt; &lt;value&gt; &lt;xsl:attribute name="attribute"&gt;content_type&lt;/xsl:attribute&gt; &lt;xsl:value-of select="MT[@N='content_type']/@V"/&gt; &lt;/value&gt; &lt;/mapping&gt; &lt;!--mapping&gt; &lt;key&gt;&lt;xsl:value-of select="MT[@N='content_id']/@V"/&gt;&lt;/key&gt; &lt;value&gt; &lt;xsl:attribute name="attribute"&gt;modified_timestamp&lt;/xsl:attribute&gt; &lt;xsl:value-of select="format-dateTime(MT[@N='modified_timestamp']/@V, '[Y0001]-[M01]-[D01]')"/&gt; &lt;/value&gt; &lt;/mapping--&gt; &lt;/xsl:template&gt; &lt;xsl:template match="mapping"&gt; &lt;xsl:for-each-group select="." group-by="value"&gt; &lt;reduction&gt; &lt;group&gt;&lt;xsl:value-of select="current-grouping-key()"/&gt;&lt;/group&gt; &lt;xsl:for-each-group select="current-group()/key" group-by="."&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:for-each-group&gt; &lt;/reduction&gt; &lt;/xsl:for-each-group&gt; &lt;/xsl:template&gt; &lt;/xsl:transform&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