I'm not sure from your description exactly what output you want.
Don't think in terms of looping and conditionals. That's procedural thinking.
This is a grouping problem. In XSLT 2.0 it's something like
<xsl:template match="Array1">
<xsl:for-each-group select="Value" group-adjacent="@D3">
<tr>
<xsl:for-each select="current-group()">
<td>... output an item in this group
In 1.0 you use the same kind of logic but you have to identify the group boundaries by hand:
<xsl:template match="Array1">
<xsl:for-each select="Value[not(@D3 = preceding-sibling::Value[1]/@D3">
<tr>
<xsl:for-each select=". | following-sibling::Value[position() < 4">
<td>
... output an item in this group
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference