Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Here are two different solutions</strong>: a simple one and a more complicated one that produces only the unique countries:</p> <p>.1. Get all <code>Country</code> elements:</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: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="*[not(self::Country)]"&gt; &lt;xsl:apply-templates select="*"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when this transformation is applied on the provided XML document, the wanted, correct result is produced</strong>:</p> <pre><code>&lt;Country&gt;Germany&lt;/Country&gt; &lt;Country&gt;Mexico&lt;/Country&gt; </code></pre> <p>.2. <strong>Find all unique countries</strong>:</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:key name="kCountryByVal" match="Country" use="."/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:copy-of select= "/*/*/Country[generate-id() = generate-id(key('kCountryByVal',.)[1]) ] "/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when this transformation is applied on any XML document with the structure of the provided document where some <code>Country</code> elements may have the same value, only one <code>Country</code> element for each different value is produced</strong>.</p> <p>With the provided XML document we still get the same result:</p> <pre><code>&lt;Country&gt;Germany&lt;/Country&gt; &lt;Country&gt;Mexico&lt;/Country&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