<pre><code>&lt;xsl:for-each select="./node [position() &amp;lt;= (count(*) div 2)]"&gt;
    &lt;li&gt;foo&lt;/li&gt;
&lt;/xsl:for-each&gt;
&lt;xsl:for-each select="./node [count(*) div 2 &amp;lt; position()]"&gt;
    &lt;li&gt;bar&lt;/li&gt;
&lt;/xsl:for-each&gt;
</code></pre>

<p>My list has 12 nodes, but the second list is always 8 and the first is always 4. What's wrong with my selects?</p>

[{'answer_id': 34111, 'author': 'A. Rex', 'author_id': 3508, 'author_profile': 'https://Stackoverflow.com/users/3508', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not at all sure, but it seems to me that <code>count(*)</code> is not doing what you think it is.  That counts the number of children of the current node, not the size of the current node list.  Could you print it out to check that it's 8 or 9 instead of 12?</p>\n\n<p>Use <code>last()</code> to get the context size.</p>\n"}, {'answer_id': 34114, 'author': 'jelovirt', 'author_id': 2679, 'author_profile': 'https://Stackoverflow.com/users/2679', 'pm_score': 4, 'selected': True, 'text': '<p>When you do <code>count(*)</code>, the current node is the <code>node</code> element being processed. You want either <code>count(current()/node)</code> or <code>last()</code> (preferable), or just calculate the midpoint to a variable for better performance and clearer code:</p>\n\n<pre><code>&lt;xsl:variable name="nodes" select="node"/&gt;\n&lt;xsl:variable name="mid" select="count($nodes) div 2"/&gt;\n&lt;xsl:for-each select="$nodes[position() &amp;lt;= $mid]"&gt;\n  &lt;li&gt;foo&lt;/li&gt;\n&lt;/xsl:for-each&gt;\n&lt;xsl:for-each select="$nodes[$mid &amp;lt; position()]"&gt;\n  &lt;li&gt;bar&lt;/li&gt;\n&lt;/xsl:for-each&gt;\n</code></pre>\n'}, {'answer_id': 34133, 'author': 'codeape', 'author_id': 3571, 'author_profile': 'https://Stackoverflow.com/users/3571', 'pm_score': 0, 'selected': False, 'text': '<p>Try count(../node). The following will gives the correct result on my test XML file (a simple nodes root with node elements), using the xsltproc XSLT processor.</p>\n\n<pre><code>&lt;xsl:for-each select="node[position() &amp;lt;= (count(../node) div 2)]"&gt;\n    ...\n&lt;/xsl:for-each&gt;\n&lt;xsl:for-each select="node[(count(../node) div 2) &amp;lt; position()]"&gt;\n    ...\n&lt;/xsl:for-each&gt;\n</code></pre>\n'}, {'answer_id': 34159, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 2, 'selected': False, 'text': '<p>You could try using the <code>last()</code> function which will give you the size of the current context:</p>\n\n<pre><code>&lt;xsl:for-each select="./node [position() &amp;lt;= last() div 2]"&gt;\n   &lt;li&gt;foo&lt;/li&gt;\n&lt;/xsl:for-each&gt;\n&lt;xsl:for-each select="./node [last() div 2 &amp;lt; position()]"&gt;\n   &lt;li&gt;bar&lt;/li&gt;\n&lt;/xsl:for-each&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/34087', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1786/']