xsl-list
[Top] [All Lists]

Performance problem

2005-02-15 04:32:26

Hi all,

I?ve tried to find tips on how to improve the performance of my XSL file and so far did not find something meaningful.
The most time-consuming part in my xml is a big-list of items

<data>
        <item name=?abc? timestamp=?2004-10-11T15:49:31?>
                <bytes> 1062207488</bytes>
        </item>
<item name=?efd? timestamp=?2004-10-13T15:44:31?>
                <bytes>23457</bytes>
        </item>
....
</data>

I process the timestamp field as follow:
INPUT -> 2004-10-11T15:49:31   OUTPUT -> 2004-Oct-11 15:49:31

I process the bytes field as follow:
INPUT -> 1048576                    OUTPUT -> 1,013MB

And display it all in a table.

My xsl file:
<xsl:template match="data">
<table border="1" cellspacing="0" cellpadding="3">
        <tr>
                <th>Name</th>
                <th>Bytes</th>
                <th>Time Stamp</th>
        </tr>
        <xsl:for-each select="item">
        <tr>
        <td nowrap="1">
                <xsl:value-of select="@name"/>
        </td>
        <td>
                <xsl:choose>
                <xsl:when test="bytes">
                        <xsl:call-template name="format-bytes">
                                <xsl:with-param name="bytes_cnt" 
select="bytes"/>
                        </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                        <xsl:text>&#160;</xsl:text>
                </xsl:otherwise>
                </xsl:choose>
        </td>
        <td nowrap="1">
                <xsl:call-template name="format-date">
                        <xsl:with-param name="date-time" select="@timestamp"/>
                </xsl:call-template>
        </td>
        </tr>
        </xsl:for-each>
        </table>
</xsl:template>

<!-- prints-out bytes count -->
<xsl:variable name="Mega" select="1024 * 1024"/>
<xsl:variable name="Giga" select="1024 * $Mega"/>

<xsl:template name="format-bytes">
<xsl:param name="cnt_bytes" select="."/>
<xsl:choose>
<xsl:when test="$ cnt_bytes &lt; 1024"><xsl:value-of select="format-number($cnt_bytes, '#,##0')"/>Bytes</xsl:when> <xsl:when test="$ cnt_bytes &lt; $Mega"><xsl:value-of select="format-number($cnt_bytes div 1024, '#,###.##')"/>KB</xsl:when> <xsl:when test="$ cnt_bytes &lt; $Giga"><xsl:value-of select="format-number($cnt_bytes div $Mega, '#,###.##')"/>MB</xsl:when> <xsl:when test="$ cnt_bytes"><xsl:value-of select="format-number($cnt_bytes div $Giga, '#,###.##')"/>GB</xsl:when>
        <xsl:otherwise><xsl:text>&#160;</xsl:text></xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- prints-out timestamp in format : dd-mon-yy H:M:S -->

<xsl:template name="format-date">
<xsl:param name="date-time" select="."/>
<xsl:choose>
<xsl:when test="$date-time">
        <xsl:value-of select="substring($date-time, 9 ,2)"/>
        <xsl:text>-</xsl:text>
        <xsl:variable name="month" select="substring($date-time, 6 ,2)"/>
        <xsl:choose>
                <xsl:when test="$month = 1">Jan</xsl:when>
                <xsl:when test="$month = 2">Feb</xsl:when>
                <xsl:when test="$month = 3">Mar</xsl:when>
                <xsl:when test="$month = 4">Apr</xsl:when>
                <xsl:when test="$month = 5">May</xsl:when>
                <xsl:when test="$month = 6">Jun</xsl:when>
                <xsl:when test="$month = 7">Jul</xsl:when>
                <xsl:when test="$month = 8">Aug</xsl:when>
                <xsl:when test="$month = 9">Sen</xsl:when>
                <xsl:when test="$month = 10">Oct</xsl:when>
                <xsl:when test="$month = 11">Nov</xsl:when>
                <xsl:when test="$month = 12">Dec</xsl:when>
        </xsl:choose>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="substring($date-time, 3 ,2)"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="substring($date-time, 12 ,8)"/>
</xsl:when>
<xsl:otherwise>
        <xsl:text>&#160;</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

1. How can I improve this code?
2. Should I use the <for-each> or <xsl:template match=?item?> ?
3. In 1/3 of the times the ?bytes? element doesn?t appears in the xml file, that is why I put a check before calling the "format-bytes" template, do you think is it best to always call the template and then check inside of the template if it exists ? 4. Do you think I should use xsl:template or xsl:function to format my timestamp/bytes ?

Best Regards,
ficfic

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>