Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>It can be as simple as this, assuming a <code>BarName</code> precedes its corresponding <code>FooName</code>:</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:template match="/*"&gt; &lt;Out&gt; &lt;Foos&gt; &lt;xsl:apply-templates select="FooName"/&gt; &lt;/Foos&gt; &lt;Bars&gt; &lt;xsl:apply-templates select="BarName"/&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:call-template name="makeFooId"/&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="BarName"&gt; &lt;Bar&gt; &lt;Id&gt;&lt;xsl:value-of select="preceding-sibling::BarId[1]"/&gt;&lt;/Id&gt; &lt;Name&gt;&lt;xsl:value-of select="."/&gt;&lt;/Name&gt; &lt;FooId&gt; &lt;xsl:call-template name="makeFooId"&gt; &lt;xsl:with-param name="pFoo" select="following-sibling::FooName[1]"/&gt; &lt;/xsl:call-template&gt; &lt;/FooId&gt; &lt;/Bar&gt; &lt;/xsl:template&gt; &lt;xsl:template name="makeFooId"&gt; &lt;xsl:param name="pFoo" select="."/&gt; &lt;!-- Replace the following line with your id - generating code --&gt; &lt;xsl:text&gt;100&lt;/xsl:text&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied to the provided XML document:</strong></p> <pre><code>&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; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>&lt;Out&gt; &lt;Foos&gt; &lt;Foo&gt; &lt;Id&gt;100&lt;/Id&gt; &lt;Name&gt;dummy&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;100&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