Note that there are some explanatory texts on larger screens.

plurals
  1. POxsl for-each loop not working correctly(logic)
    text
    copied!<p>I have applied one logic in xsl for-each loop, but it is not behaving correctly. Can anyone help me out.</p> <p>XML</p> <pre><code>&lt;a&gt; &lt;b&gt; &lt;c&gt; &lt;string&gt;16&lt;/string&gt; &lt;string&gt;4&lt;/string&gt; &lt;string&gt;id&lt;/string&gt; &lt;int&gt;123&lt;/int&gt; &lt;/c&gt; &lt;c&gt; &lt;string&gt;16&lt;/string&gt; &lt;string&gt;4&lt;/string&gt; &lt;string&gt;id&lt;/string&gt; &lt;int&gt;123&lt;/int&gt; &lt;/c&gt; &lt;/b&gt; &lt;/a&gt; </code></pre> <p>XSL</p> <pre><code>&lt;xsl:for-each select="/a/b/c"&gt; &lt;c&gt; &lt;xsl:for-each select="/a/b/c/string"&gt;" &lt;xsl:variable name ="pos" select="position()"/&gt; &lt;xsl:if test="position() mod 2!=0"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="self::node()[text()='16']"&gt; &lt;int&gt;16&lt;/int&gt;&lt;int&gt;4&lt;/int&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:element name="{/a/b/c/string[position()=$pos]}"&gt;" &lt;xsl:value-of select="/a/b/c/string[position()=$pos+1]\/&gt; &lt;/xsl:element&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&gt; &lt;/c&gt; &lt;/xsl:for-each&gt; </code></pre> <p>Desired Output</p> <pre><code> &lt;a&gt; &lt;b&gt; &lt;c&gt; &lt;int&gt;16&lt;/int&gt; &lt;int&gt;4&lt;/int&gt; &lt;id&gt;123&lt;/id&gt; &lt;/c&gt; &lt;c&gt; &lt;int&gt;16&lt;/int&gt; &lt;int&gt;4&lt;/int&gt; &lt;id&gt;123&lt;/id&gt; &lt;/c&gt; &lt;/b&gt; &lt;/a&gt; </code></pre> <p>Actual Output</p> <pre><code>&lt;a&gt; &lt;b&gt; &lt;c&gt; &lt;int&gt;16&lt;/int&gt; &lt;int&gt;4&lt;/int&gt; &lt;id&gt;123&lt;/id&gt; &lt;int&gt;16&lt;/int&gt; &lt;int&gt;4&lt;/int&gt; &lt;/c&gt; &lt;c&gt; &lt;int&gt;16&lt;/int&gt; &lt;int&gt;4&lt;/int&gt; &lt;id&gt;123&lt;/id&gt; &lt;int&gt;16&lt;/int&gt; &lt;int&gt;4&lt;/int&gt; &lt;/c&gt; &lt;/b&gt; &lt;/a&gt; </code></pre> <p>There is some problem in inner for-each loop but I am not able to find out</p>
 

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