You could match on sec/sec, sec/sec/sec, sec/sec/sec/sec and assign the border color as needed:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:param name="bg1" select="'red'"/>
<xsl:param name="bg2" select="'green'"/>
<xsl:param name="bg3" select="'blue'"/>
<xsl:param name="bg4" select="'yellow'"/>
<xsl:template match="sec[not(ancestor::sec)]">
<div style="border: 1px solid {$bg1};">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="sec/sec">
<div style="border: 1px solid {$bg2};">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="sec/sec/sec">
<div style="border: 1px solid {$bg3};">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="sec/sec/sec/sec">
<div style="border: 1px solid {$bg4};">
<xsl:apply-templates/>
</div>
</xsl:template>
</xsl:stylesheet>