Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am assuming you want to output a <code>Foo</code> element with a unique ID for each distinct <code>FooName</code> in the input. For this, you could use the <a href="http://www.jenitennison.com/xslt/grouping/muenchian.html" rel="nofollow">Muenchian method</a>:</p> <p>First you'd build a <a href="http://www.w3.org/TR/xslt/#key" rel="nofollow"><code>xsl:key</code></a> using which you can quickly retrieve <code>FooName</code> elements by their content.</p> <pre><code>&lt;xsl:key name="FooNames" match="FooName" use="."/&gt; </code></pre> <p>Using this key, you can then process only the first instance of each FooName, thus getting a distinct list. Here you can make use of the <code>generate-id()</code> function for comparing nodes for identity.</p> <pre><code>&lt;xsl:apply-templates select="//FooName [generate-id() = generate-id(key('FooNames', .)[1])]"/&gt; </code></pre> <p>For generating the identifiers, if you don't care that the generated identifiers may vary from run to run, you can again use the <code>generate-id()</code> function. </p> <p>Putting it all together:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/&gt; &lt;xsl:key name="FooNames" match="FooName" use="."/&gt; &lt;xsl:template match="/"&gt; &lt;Out&gt; &lt;Foos&gt; &lt;xsl:apply-templates select="//FooName[generate-id() = generate-id(key('FooNames', .)[1])]"/&gt; &lt;/Foos&gt; &lt;Bars&gt; &lt;xsl:apply-templates select="//Input"/&gt; &lt;/Bars&gt; &lt;/Out&gt; &lt;/xsl:template&gt; &lt;xsl:template match="FooName"&gt; &lt;Foo&gt; &lt;Id&gt;&lt;xsl:value-of select="generate-id()"/&gt;&lt;/Id&gt; &lt;Name&gt;&lt;xsl:value-of select="."/&gt;&lt;/Name&gt; &lt;/Foo&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Input"&gt; &lt;Bar&gt; &lt;Id&gt;&lt;xsl:value-of select="BarId"/&gt;&lt;/Id&gt; &lt;Name&gt;&lt;xsl:value-of select="BarName"/&gt;&lt;/Name&gt; &lt;FooId&gt; &lt;xsl:value-of select="generate-id(key('FooNames', FooName)[1])"/&gt; &lt;/FooId&gt; &lt;/Bar&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>With the following sample input: </p> <pre><code>&lt;Inputs&gt; &lt;Input&gt; &lt;BarId&gt;123&lt;/BarId&gt; &lt;BarName&gt;myname&lt;/BarName&gt; &lt;FooName&gt;dummy&lt;/FooName&gt; &lt;/Input&gt; &lt;Input&gt; &lt;BarId&gt;124&lt;/BarId&gt; &lt;BarName&gt;anothername&lt;/BarName&gt; &lt;FooName&gt;dummy&lt;/FooName&gt; &lt;/Input&gt; &lt;Input&gt; &lt;BarId&gt;125&lt;/BarId&gt; &lt;BarName&gt;yetanothername&lt;/BarName&gt; &lt;FooName&gt;dummy-two&lt;/FooName&gt; &lt;/Input&gt; &lt;/Inputs&gt; </code></pre> <p>It will yield the output:</p> <pre><code>&lt;Out&gt; &lt;Foos&gt; &lt;Foo&gt; &lt;Id&gt;id213296&lt;/Id&gt; &lt;Name&gt;dummy&lt;/Name&gt; &lt;/Foo&gt; &lt;Foo&gt; &lt;Id&gt;id214097&lt;/Id&gt; &lt;Name&gt;dummy-two&lt;/Name&gt; &lt;/Foo&gt; &lt;/Foos&gt; &lt;Bars&gt; &lt;Bar&gt; &lt;Id&gt;123&lt;/Id&gt; &lt;Name&gt;myname&lt;/Name&gt; &lt;FooId&gt;id213296&lt;/FooId&gt; &lt;/Bar&gt; &lt;Bar&gt; &lt;Id&gt;124&lt;/Id&gt; &lt;Name&gt;anothername&lt;/Name&gt; &lt;FooId&gt;id213296&lt;/FooId&gt; &lt;/Bar&gt; &lt;Bar&gt; &lt;Id&gt;125&lt;/Id&gt; &lt;Name&gt;yetanothername&lt;/Name&gt; &lt;FooId&gt;id214097&lt;/FooId&gt; &lt;/Bar&gt; &lt;/Bars&gt; &lt;/Out&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