I am fairly new to XSL and am looking to optimize my code. What I need to do is vary the output of my XSL if the item count is 3 or more than 3. If there are 3 items, each \[code\]<div/>\[/code\] should have only 1 \[code\]<a/>\[/code\] element. If there are 6 the \[code\]<div/>\[/code\] should contain 2 \[code\]<a/>\[/code\] elements. The code I have below works, but I know there has to be a better way.If 3 items it looks like this\[code\]<div> <div> <a href="http://stackoverflow.com/link1.html"> <div> <h1>Header 1</h1> <p>Body coy 1</p> </div> </a> </div> <div> <a href="http://stackoverflow.com/link2.html"> <div> <h1>Header 2</h1> <p>Body coy 2</p> </div> </a> </div> <div> <a href="http://stackoverflow.com/link3.html"> <div> <h1>Header 3</h1> <p>Body coy 3</p> </div> </a> </div></div>\[/code\]If there are 6 items it should like this:\[code\]<div> <div> <a href="http://stackoverflow.com/link1.html"> <div> <h1>Header 1</h1> <p>Body coy 1</p> </div> </a> <a href="http://stackoverflow.com/link2.html"> <div> <h1>Header 2</h1> <p>Body coy 2</p> </div> </a> </div> <div> <a href="http://stackoverflow.com/link3.html"> <div> <h1>Header 3</h1> <p>Body coy 3</p> </div> </a> <a href="http://stackoverflow.com/link4.html"> <div> <h1>Header 4</h1> <p>Body coy 4</p> </div> </a> </div> <div> <a href="http://stackoverflow.com/link5.html"> <div> <h1>Header 5</h1> <p>Body coy 5</p> </div> </a> <a href="http://stackoverflow.com/link6.html"> <div> <h1>Header 6</h1> <p>Body coy 6</p> </div> </a> </div></div>\[/code\]XML (6 items):\[code\]<?xml version="1.0" encoding="UTF-8"?><root> <article> <header>Header 1</header> <body>Body coy 1</body> <url><a href="http://stackoverflow.com/link1.html" title="Link1">Link 1</a></url> </article> <article> <header>Header 2</header> <body>Body coy 2</body> <url><a href="http://stackoverflow.com/link2.html" title="Link2">Link 2</a></url> </article> <article> <header>Header 3</header> <body>Body coy 3</body> <url><a href="http://stackoverflow.com/link3.html" title="Link1">Link 3</a></url> </article> <article> <header>Header 4</header> <body>Body coy 4</body> <url><a href="http://stackoverflow.com/link4.html" title="Link4">Link 4</a></url> </article> <article> <header>Header 5</header> <body>Body coy 5</body> <url><a href="http://stackoverflow.com/link5.html" title="Link1">Link 5</a></url> </article> <article> <header>Header 6</header> <body>Body coy 6</body> <url><a href="http://stackoverflow.com/link6.html" title="Link1">Link 6</a></url> </article></root>\[/code\]XML (3 items):\[code\]<?xml version="1.0" encoding="UTF-8"?><root> <article> <header>Header 1</header> <body>Body coy 1</body> <url><a href="http://stackoverflow.com/link1.html" title="Link1">Link 1</a></url> </article> <article> <header>Header 2</header> <body>Body coy 2</body> <url><a href="http://stackoverflow.com/link2.html" title="Link2">Link 2</a></url> </article> <article> <header>Header 3</header> <body>Body coy 3</body> <url><a href="http://stackoverflow.com/link3.html" title="Link1">Link 3</a></url> </article></root>\[/code\]XSL:\[code\]<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- the number of items to include in each group --> <xslutput omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/root"> <div> <xsl:variable name="articleCount" select="count(article)"/> <xsl:variable name="group" select="$articleCount div 3" /> <xsl:choose> <xsl:when test="$articleCount = 3"> <xsl:for-each select="article"> <div> <xsl:call-template name="articleItem"> <xsl:with-param name="item" select="."/> </xsl:call-template> </div> </xsl:for-each> </xsl:when> <xsltherwise> <xsl:apply-templates select="article[position() mod 2 = 1]" > <xsl:with-param name="group" select="2"/> </xsl:apply-templates> </xsltherwise> </xsl:choose> </div> </xsl:template> <xsl:template match="article" mode="inner"> <xslaram name="group"/> <!-- handle items appropriately here --> <xsl:call-template name="articleItem"> <xsl:with-param name="item" select="."/> </xsl:call-template> </xsl:template> <xsl:template match="article"> <xslaram name="group"/> <div class="article"> <xsl:apply-templates select=".|following-sibling::article[$group > position()]" mode="inner" /> </div> </xsl:template> <xsl:template name="articleItem"> <xslaram name="item"/> <a> <xsl:attribute name="href"><xsl:value-of select="url/a/@href" /></xsl:attribute> <xsl:call-template name="articleContent"> <xsl:with-param name="item" select="."/> </xsl:call-template> </a> </xsl:template> <xsl:template name="articleContent"> <xslaram name="item"/> <div class="article-copy"> <h1><xsl:value-of disable-output-escaping="yes" select="header" /></h1> <p><xsl:value-of disable-output-escaping="yes" select="body" /></p> </div> </xsl:template></xsl:stylesheet>\[/code\]