...

View Full Version : Resolved XSLT - Convert string - Bytes to KB or MB



fidgen
06-09-2009, 10:17 AM
Hiya,

I'm trying to format a SharePoint webpart.

I need to convert a string which contains numerical data - the filesize in bytes - into kb or Mb as appropriate. I'm using the following code, but it appears to break SharePoint. I think it's the use of the < and > which is the problem?


&gt; and &lt;



<xsl:variable name="FileSizeKb">
<xsl:if test="string-length(@FileSizeDisplay) &gt; 0">
<xsl:if test="number(@FileSizeDisplay) &gt; 0">
<xsl:choose>
<xsl:when test="round(@FileSizeDisplay div 1024) &lt; 1"><xsl:value-of select="@FileSizeDisplay" /> Bytes</xsl:when>
<xsl:when test="round(@FileSizeDisplay div (1024 *1024)) &lt; 1"><xsl:value-of select="round(@FileSizeDisplay div 1024)" />kb</xsl:when>
<xsl:otherwise><xsl:value-of select="round(@FileSizeDisplay div (1024 * 1024))" />Mb</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:if>
</xsl:variable>


@FileSizeDisplay is declared and able to be used in this template - if you call it directly it displays the string. EG 4865121

Thanks for any hints :)

fidgen
06-09-2009, 10:51 AM
Wait.. there was a rogue comma!

Here's the working code - it includes a better section for calculating the filesizes down to one decimal place. Much neater and more accurate tbh.


<xsl:variable name="FileSizeKb">

<xsl:if test="string-length(@FileSizeDisplay) &gt; 0">

<xsl:if test="number(@FileSizeDisplay) &gt; 0">

<xsl:choose>

<xsl:when test="round(@FileSizeDisplay div 1024) &lt; 1"><xsl:value-of select="@FileSizeDisplay" /> Bytes</xsl:when>

<xsl:when test="round(@FileSizeDisplay div 1048576) &lt; 1"><xsl:value-of select="format-number((@FileSizeDisplay div 1024), '0.0')" />kb</xsl:when>

<xsl:otherwise><xsl:value-of select="format-number((@FileSizeDisplay div 1048576), '0.00')" />Mb</xsl:otherwise>

</xsl:choose>

</xsl:if>

</xsl:if>

</xsl:variable>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum