Template to determine file size notation

Feb 9, 2011 at 4:17 PM

This template works by passing in how many bytes are within your file size.  It then recurses the template to determine if it's KB, MB, GB, and so on...  I couldn't think of a good name for the counter variable, so I'm up for suggestions.  Also, is there a way I can get around the need to pass in counter?  I'd like to simply pass in @FileSize and have the template figure out the rest.

Here's how to use the template:

<xsl:call-template name="CalculateByteNotation">
	<xsl:with-param name="fileSize" select="@File_x0020_Size" />
	<xsl:with-param name="counter" select="0" />
</xsl:call-template>
<xsl:template name="CalculateByteNotation">
		<xsl:param name="fileSize" />
		<xsl:param name="counter" />
		<!-- I know there are purists out there, so feel free to change the assumption that a KB 
			is represented as 10^3. Feel free to change it to suite your needs.
			http://en.wikipedia.org/wiki/Megabyte
		-->
			<!-- Divide fileSize by 1000 and start recursion, if needed -->
			<xsl:choose>
				<xsl:when test="format-number($fileSize, '###.#') &gt; 999.9">
					<xsl:call-template name="CalculateByteNotation">
						<xsl:with-param name="fileSize" select="format-number($fileSize div 1000, '###.#')" />
						<xsl:with-param name="counter" select="$counter + 1" />
					</xsl:call-template>	
				</xsl:when>
				<xsl:otherwise>
					<!-- Append the Byte notation -->
					<xsl:choose>
						<xsl:when test="$counter = 0">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>Bytes
						</xsl:when>
						<xsl:when test="$counter = 1">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>KB
						</xsl:when>
						<xsl:when test="$counter = 2">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>MB
						</xsl:when>
						<xsl:when test="$counter = 3">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>GB
						</xsl:when>
						<xsl:when test="$counter = 4">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>TB
						</xsl:when>
						<xsl:when test="$counter = 5">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>PB
						</xsl:when>
						<xsl:when test="$counter = 6">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>EB
						</xsl:when>
						<xsl:when test="$counter = 7">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>ZB
						</xsl:when>
						<xsl:when test="$counter = 8">
							<xsl:value-of select="$fileSize" />
							<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>YB
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="'Seriously?'" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
	</xsl:template>

Coordinator
Feb 10, 2011 at 8:05 PM

Matt:

I think you've made this overly complex. There doesn't seem to be any reason to do the recursion. What about something like this?

<xsl:template name="FormatFileSize">
 <xsl:param name="FileSize" />
 <xsl:choose>
  <xsl:when test="@FileSize &gt; 1099511627776">
   <xsl:value-of select="concat(format-number($FileSize div 1099511627776, '###.#'), 'TB')"/>
  </xsl:when>
  <xsl:when test="@FileSize &gt; 1073741824">
   <xsl:value-of select="concat(format-number($FileSize div 1073741824, '###.#'), 'GB')"/>
  </xsl:when>
  <xsl:when test="@FileSize &gt; 1048576">
   <xsl:value-of select="concat(format-number($FileSize div 1048576, '###.#'), 'MB')"/>
  </xsl:when>
  <xsl:when test="@FileSize &gt; 1024">
   <xsl:value-of select="concat(format-number($FileSize div 1024, '###.#'), 'KB')"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="concat(format-number($FileSize, '###.#'), 'Bytes')"/>  
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>
M.
Feb 11, 2011 at 2:00 PM

Man, that is much easier...