For the time conversion, here is something which I find simpler:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="time/text()">
<xsl:variable name="vZ" select="(.+12) mod 12"/>
<xsl:value-of select="$vZ - ($vZ -12)*($vZ=0)"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<t>
<time>-3</time>
<time>0</time>
<time>7</time>
<time>12</time>
<time>17</time>
<time>24</time>
</t>
the wanted, correct result is produced:
<t>
<time>9</time>
<time>12</time>
<time>7</time>
<time>12</time>
<time>5</time>
<time>12</time>
</t>
For am/pm (if my understanding for the edge cases is correct) we add this code:
<xsl:variable name="vNorm" select=
"not(. >= 0)*(24 +.)
+
(. >=0 and not(. = 24))*.
+
not(. = 24)
"/>
<xsl:value-of select="$vPeriods[1+($vNorm>=12)]"/>
The complete transformation becomes:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:timePeriods>
<p>am</p>
<p>pm</p>
</my:timePeriods>
<xsl:variable name="vPeriods" select=
"document('')/*/my:timePeriods/*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="time/text()">
<xsl:variable name="vZ" select="(.+12) mod 12"/>
<xsl:value-of select="$vZ - ($vZ -12)*($vZ=0)"/>
<xsl:variable name="vNorm" select=
"not(. >= 0)*(24 +.)
+
(. >=0 and not(. = 24))*.
+
not(. = 24)
"/>
<xsl:value-of select="$vPeriods[1+($vNorm>=12)]"/>
</xsl:template>
</xsl:stylesheet>
and when applied to the same XML document (above) the result is:
<t>
<time>9pm</time>
<time>12am</time>
<time>7am</time>
<time>12pm</time>
<time>5pm</time>
<time>12am</time>
</t>