Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Minimize XSL Code

Status
Not open for further replies.

evilmungo

Programmer
May 23, 2002
2
0
0
GB
Ok I have the following XSL style sheet that works fine. The only problem is it's far too slow.

========================================================
<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
<!-- <xsl:stylesheet xmlns:xsl=&quot; -->
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;<xsl:template match=&quot;/&quot;>
<table class=&quot;Dtable&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;3&quot; width=&quot;100%&quot;>
<thead>
<th>Work Activity</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '02')
</xsl:attribute>
F
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '03')
</xsl:attribute>
M
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '04')
</xsl:attribute>
A
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '05')
</xsl:attribute>
M
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '06')
</xsl:attribute>
J
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '07')
</xsl:attribute>
J
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '08')
</xsl:attribute>
A
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '09')
</xsl:attribute>
S
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '10')
</xsl:attribute>
O
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '11')
</xsl:attribute>
N
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2002' , '12')
</xsl:attribute>
D
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2003' , '01')
</xsl:attribute>
J
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2003' , '02')
</xsl:attribute>
F
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2003' , '03')
</xsl:attribute>
M
</xsl:element>
</th>
<th>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('monthlybyworkactivity.xsl', '2003' , '04')
</xsl:attribute>
A
</xsl:element>
</th>
</thead>
<xsl:for-each select=&quot;xmldata/workactivity&quot;>
<tr>
<td>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:switchView('yearlybyquestion.xsl','<xsl:value-of select=&quot;text&quot;/>')
</xsl:attribute>
<xsl:value-of select=&quot;text&quot;/>
</xsl:element>
</td>
<td class=&quot;before&quot;>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
<xsl:for-each select=&quot;question/date[month='02' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;before&quot;>
<xsl:for-each select=&quot;question/date[month='03' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;before&quot;>
<xsl:for-each select=&quot;question/date[month='04' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;now&quot;>
<xsl:for-each select=&quot;question/date[month='05' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='06' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='07' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='08' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='09' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='10' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='11' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='12' and year='2002']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='01' and year='2003']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='02' and year='2003']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='03' and year='2003']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
<td class=&quot;after&quot;>
<xsl:for-each select=&quot;question/date[month='04' and year='2003']&quot;>
<xsl:if test=&quot;position()=1&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;>
<xsl:value-of select=&quot;status&quot;/>
</xsl:attribute>
<xsl:attribute name=&quot;alt&quot;>
<xsl:value-of select=&quot;responsible&quot;/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
<img src=&quot;/icons/ecblank.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;1&quot; height=&quot;1&quot; />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
==========================================================

Any suggestions on how to speed this up would be gratefully accepted.

Ben
 
In each table element, you select all questions for that date, but only do something with the first (position()=1). Why not just select the first element and save yourself all the looping time. Don't know how much time it will save you, but if there are a lot of questions, it may be what is taking so long.

<xsl:for-each select=&quot;(question/date[month='04' and year='2003'])[1]&quot;>

That should select only the first node of the nodeset created by your previous select statement.

Fair warning, I haven't tried this one myself.

~~~~
&quot;Common sense tells you that the world is flat.&quot;
 
could you print a small example of the input xml and output?
 
Ok, first thanks to uura, that worked great, and has probably speeded it up quite a bit.

Heres a small portion of the XML that is passed into the stylesheet.

<?xml version=&quot;1.0&quot; ?>
<xmldata>
<workactivity>
<text>Absence</text>
<question>
<text>A Review Appointment must be made for Jarvis Cocker on 03/10/2001</text>
<date>
<day>01</day>
<month>01</month>
<year>2002</year>
<responsible>John</responsible>
<status>2</status>
</date>
<date>
<day>11</day>
<month>01</month>
<year>2002</year>
<responsible>Steve</responsible>
<status>1</status>
</date>
<date>
<day>11</day>
<month>02</month>
<year>2002</year>
<responsible>Mark</responsible>
<status>4</status>
</date>
</question>
<question>
<text>My second question</text>
<date>
<day>21</day>
<month>11</month>
<year>2001</year>
<responsible>John</responsible>
<status>3</status>
</date>
<date>
<day>07</day>
<month>07</month>
<year>2002</year>
<responsible>Mark</responsible>
<status>1</status>
</date>
</question>
</workactivity>
</xmldata>

 
there's no real reason why this should run slow.
if each question had many date fields then the
<xsl:for-each select=&quot;question/date[month='04' and year='2003']&quot;>

would start to cause problems since for each month you are rechecking the whole list. if the dates are always sorted then you might be able to use some shortcuts. i don't really understand the test for 'position()=1' - is it important only to print the first item for each month?

some tricks to help speed up the code a little - but reduce the size a lot would be to print the headers like....

<th>
<a href=&quot;javascript:switchView('monthlybyworkactivity.xsl', '2002' , '02')&quot;>F</a>
</th>

and then to print the date information you could use...

<xsl:variable name=&quot;date&quot; select=&quot;question/date[month='03' and year='2003']&quot;/>
<img src=&quot;{$date/status}&quot; alt=&quot;{$date/responsible}&quot;/>

if there is more than one date for that month it will just select the first one by default (test this tho as i'm doing this from memory)

to make the output more readable it is also useful to add

<xsl:eek:utput method=&quot;xml&quot; indent=&quot;yes&quot;/>

to the top of the doc, under the initial <xsl:stylesheet> tag.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top