2

I am new to XSLT.I have an XML document and I am using the XSL for converting the XML in to an HTML table.The XML is response from an server to web client.In this case It is IE9 browser.The XSLT processing is done by browser.The number of "ch3" nodes ranges from 1 to 100000.

Below is the sample code of what I am doing .

In the below xsl code the variable is created in every loop.I like to know what is the effect of this creation on the browser memory.Also will this have any performance impacts?

    ============XMLDoc=======
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <root>
    <ch1>
      <ch2>
       <ch3 a="bosy" b="" c="5" d="nobody"/>
        ......        

     </ch2>
    </ch1>
    </root>

     ============XMLDoc=======
      ============XSLSheet=======

     <?xml version="1.0" encoding="ISO-8859-1"?>
     <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="root/ch1">
        ......
      <xsl:for-each select="ch1/ch">
        <xsl:variable name="color">
         <xsl:choose>
         <xsl:when test="@c = '5'">orange</xsl:when>
          <xsl:when test="@c = '4'">red</xsl:when>
         <xsl:when test="@c = '3'">white</xsl:when>
         <xsl:when test="@c = '2'">gree</xsl:when>
         <xsl:when test="@c = '1'">yellow</xsl:when>
         <xsl:when test="@c = '0'">blue</xsl:when>
        </xsl:choose>
       </xsl:variable>
         .............
     </xsl:for-each>
      .............
      <xsl-template>

    </xsl:transform>

      ============XSL Sheet=======
loki
  • 21
  • 5
  • 1
    Chances are you don't need the variable at all. What do you need the color for? For styling or for table content? Also, there are some "stylistic" issues with your code. If we could see more, we would be able to advice you about some XSLT best practices (e.g. avoid for-each/choose, use template matches instead). – Thomas W Nov 28 '12 at 06:56

3 Answers3

0

I think XSLT variables are pretty cheap. In general, I wouldn't worry about the performance unless you actually have a problem with it. You might want to generate one of these 100000-node documents to see if the performance is acceptable.

djc
  • 11,603
  • 5
  • 41
  • 54
0

That's definitely implementation specific, it'll potentially vary between browsers.

However, a variable in this context is no longer required after each iteration of the for-each loop, so any self respecting XSLT processor should never need to have 100000 values in memory, the memory can be freed after each iteration.

Flynn1179
  • 11,925
  • 6
  • 38
  • 74
  • Flynn1179, He is doing something represented by the elipsis inside of `xsl:for-each` -- the argument that "the memory can be freed after each iteration" can't help the fact that the maximum memory requirement at some points in the transformation and for probably significant time durations can be high. – Dimitre Novatchev Nov 28 '12 at 13:43
  • Quite possibly, but that's nothing to do with the question, or my answer. He asked if the variable being created 100000 times would cause memory problems, not if any other part of the transformation will. – Flynn1179 Nov 28 '12 at 16:19
  • And even if so, multiple freeing and allocating memory typically leads to memory fragmentation and causes more work (and more frequently) for the garbage collector. – Dimitre Novatchev Nov 28 '12 at 17:24
0

You need to address performance top-down not bottom-up. Do you know your performance requirements? Can you measure the performance you are currently achieving? Is there a gap, and if so, can you quantify it? At this stage you can start drilling down to analyze the causes, e.g. by doing controlled experiments to measure the effect of changing the design in particular ways. Speculating as to whether particular constructs are inefficient is part of this process, but the only way to get an answer is to make measurements.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164