Sorting of Form Fields not Working Correctly

I have a Contour form (Razor Macro) with conditional fields and I am using the XSLT transformed email workflow to display my field results in an email, but some of the fields are not displayed the the order they are on the form within the email. Here is my XSLT file that I am using; could someone take a look at it and see why some fields are not in the order that I placed them on the form?

<xsl:param name="records"/>
<xsl:template match="/">
<table style="border-collapse:collapse;border:1px solid black;">
<xsl:for-each select="$records//fields/child::*">
<xsl:sort select="./@pageindex" order="ascending"/>
<xsl:sort select="./@fieldsetindex" order="ascending"/>
<xsl:sort select="./@sortorder" order="ascending"/>
<tr style="border:1px solid black;">
<td valign="top" style="border:1px solid black;padding:5px;font-family:Verdana,Helvetica,Arial,sans-serif;font-size:11px;">
<strong>
<xsl:value-of select="./caption"/>
</strong>
</td>
<td valign="top" style="border:1px solid black;padding:5px;font-family:Verdana,Helvetica,Arial,sans-serif;font-size:11px;">
<xsl:choose>
<xsl:when test="contains(.//value, '/umbraco/plugins/umbracoContour/files/')">
<a href="http://{umbraco.library:RequestServerVariables('SERVER_NAME')}{.//value}">
<xsl:value-of select=".//value"/>
</a>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="./values/value">
<xsl:if test="position() > 1">
<br/>
</xsl:if>
<xsl:value-of select="umbraco.library:ReplaceLineBreaks(.)"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/59670-sorting-of-form-fields-not-working-correctly