 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 6th, 2007, 08:43 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
loop in a function
Hello
I am trying to write a function that suppose to have an inside counter that updated after each loop. I will try to explain with an example:
counter = 10.
for(temp1>0)
{ for the 1st 4
do something with name=current1
temp1--
)
for the second 4 do the same thing, but now give name=current2 and so on
until temp1=0.
I am having difficulties to do the inside loop and to write the method. the method that I have is:
<xsl:if test = "$counter-db_columns[@class='statistic']">
<xsl:for-each select = "counter > 0">
<case>
<xsl:attribute name = "v2">
<xsl:value-of select="concat'STATISTIC_COUNTER_', $current)"/>
</xsl:attribute>
<xsl:for-each select = "$counter-db_columns[@class='statistic']">
<column>
<xsl:attribute name = "name">
<xsl:value-of select="@dbcolumn"/>
</xsl:attribute>
</column>
</xsl:for-each>
</case>
</xsl:for-each>
</xsl:if>
Hope it was clear enough
Thank for the help
Kfir
|
|

September 6th, 2007, 09:02 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
XSLT is a functional language, not a procedural one, and you are attacking your problem in a procedural way. Instead of describing the code you want to write (and can't write), it's better to describe what you want your function to do: what is its input, and what is it's output?
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

September 6th, 2007, 09:08 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the input is:
10 times the for will run on $counter-db_columns[@class='statistic']
(I have 10 classes of name statistic).
the required out put is:
divide this input $counter-db_columns[@class='statistic'] into groups of 4.
to each group to give an unique name.
I should have 3 groups (first 2 groups with 4 classes, and one group with 2 classes).
each should have a unique name.
hope that this is more clear and correct in XSLT matter.
|
|

September 6th, 2007, 09:19 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Try this:
<xsl:for-each select="$counter-db_columns[@class='statistic'][position() mod 4 = 1]">
<group name="name{position()}">
<xsl:variable name="start" select="(position()-1)*4 + 1"/>
<xsl:copy-of select="$counter-db_columns[@class='statistic'][position() >= $start and position() < $start+4]"/>
</group>
</xsl:for-each>
It can be made a bit less verbose in XSLT 2.0
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

September 8th, 2007, 11:49 PM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
this is correct in general, but it still doesnt solve the main problem. since in the input there are a lot of $class=statistic, I run in a for loop to get over all the $claa and to rap it in a counter and I get more counters then needed.
the input:
<xsl:for-each select = "$counter-db_columns[@class='statistic']">
<xsl:call-template name="common-time-display-statistic">
<xsl:with-param name="v2" select="@dbcolumn"/>
<xsl:with-param name="display" select="/etd/counters/counter/data[@class='statistic']/../display/@sum-format"/>
<xsl:with-param name="column_key" select="@dbcolumn"/>
<xsl:with-param name="column_display" select="concat($tech_id,'-', @dbcolumn)"/>
</xsl:call-template>
</xsl:for-each>
<xsl:template name="common-time-display-statistic">
<xsl:param name="v2" />
<xsl:param name="display" />
<xsl:param name="baseline_column_key" />
<xsl:param name="baseline_column_display" />
<xsl:param name="column_key" />
<xsl:param name="column_display" />
<case name="statistic_counter_{position()}">
<x-axis >
<xsl:attribute name="display">
<xsl:value-of select= "$display"/>
</xsl:attribute>
<columns>
<xsl:variable name="start" select="(position()-1)*4 + 1"/>
<xsl:for-each select = "$counter-db_columns[@class='statistic'][position() >= $start and position() < $start+4]">
<column showNAIfNullValues="true" showZeroIfZeroValues="true" >
<xsl:call-template name="general-attribute-parameters">
<xsl:with-param name="key" select="@dbcolumn"/>
<xsl:with-param name="display" select="concat($tech_id, '-', @dbcolumn)"/>
</xsl:call-template>
</column>
</xsl:for-each>
</columns>
</x-axis>
<legend/>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true">
<baseline>
<columns>
<xsl:variable name="start" select="(position()-1)*4 + 1"/>
<xsl:for-each select = "$counter-db_columns[@class='statistic'][position() >= $start and position() < $start+4]">
<column showNAIfNullValues="true" showZeroIfZeroValues="true" >
<xsl:call-template name="general-attribute-parameters">
<xsl:with-param name="key" select="concat(@dbcolumn, '_BL')"/>
<xsl:with-param name="display" select="concat($tech_id, '-', @dbcolumn, '_BL')"/>
</xsl:call-template>
</column>
</xsl:for-each>
</columns>
</baseline>
</case>
</select>
<tooltip show-only-tooltip-fields="true">
<columns>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true">
<xsl:variable name="start" select="(position()-1)*4 + 1"/>
<xsl:for-each select = "$counter-db_columns[@class='statistic'][position() >= $start and position() < $start+4]">
<column showNAIfNullValues="true" showZeroIfZeroValues="true" >
<xsl:call-template name="general-attribute-parameters">
<xsl:with-param name="key" select="concat(@dbcolumn, '_BL')"/>
<xsl:with-param name="display" select="concat($tech_id, '-', @dbcolumn, '_BL')"/>
</xsl:call-template>
</column>
</xsl:for-each>
</case>
</select>
<xsl:variable name="start" select="(position()-1)*4 + 1"/>
<xsl:for-each select = "$counter-db_columns[@class='statistic'][position() >= $start and position() < $start+4]">
<column showNAIfNullValues="true" showZeroIfZeroValues="true" >
<xsl:call-template name="general-attribute-parameters">
<xsl:with-param name="key" select="@dbcolumn"/>
<xsl:with-param name="display" select="concat($tech_id, '-', @dbcolumn)"/>
</xsl:call-template>
</column>
</xsl:for-each>
</columns>
</tooltip>
</case>
</xsl:template>
and the out put is:
<case name="statistic_counter_1">
<x-axis display="byte">
<columns>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ" display="I3-BYTES_READ"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE" display="I3-BYTES_WRITE"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ1" display="I3-BYTES_READ1"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE1" display="I3-BYTES_WRITE1"/>
</columns>
</x-axis>
<legend/>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true">
<baseline>
<columns>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ_BL" display="I3-BYTES_READ_BL"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE_BL" display="I3-BYTES_WRITE_BL"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ1_BL" display="I3-BYTES_READ1_BL"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE1_BL" display="I3-BYTES_WRITE1_BL"/>
</columns>
</baseline>
</case>
</select>
<tooltip show-only-tooltip-fields="true">
<columns>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true">
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ_BL" display="I3-BYTES_READ_BL"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE_BL" display="I3-BYTES_WRITE_BL"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ1_BL" display="I3-BYTES_READ1_BL"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE1_BL" display="I3-BYTES_WRITE1_BL"/>
</case>
</select>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ" display="I3-BYTES_READ"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE" display="I3-BYTES_WRITE"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ1" display="I3-BYTES_READ1"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE1" display="I3-BYTES_WRITE1"/>
</columns>
</tooltip>
</case>
<case name="statistic_counter_2">
<x-axis display="byte">
<columns>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ2" display="I3-BYTES_READ2"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE2" display="I3-BYTES_WRITE2"/>
</columns>
</x-axis>
<legend/>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true">
<baseline>
<columns>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ2_BL" display="I3-BYTES_READ2_BL"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE2_BL" display="I3-BYTES_WRITE2_BL"/>
</columns>
</baseline>
</case>
</select>
<tooltip show-only-tooltip-fields="true">
<columns>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true">
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ2_BL" display="I3-BYTES_READ2_BL"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE2_BL" display="I3-BYTES_WRITE2_BL"/>
</case>
</select>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_READ2" display="I3-BYTES_READ2"/>
<column showZeroIfZeroValues="true" showNAIfNullValues="true" key="BYTES_WRITE2" display="I3-BYTES_WRITE2"/>
</columns>
</tooltip>
</case>
<case name="statistic_counter_3">
<x-axis display="byte">
<columns/>
</x-axis>
<legend/>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true">
<baseline>
<columns/>
</baseline>
</case>
</select>
<tooltip show-only-tooltip-fields="true">
<columns>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true"/>
</select>
</columns>
</tooltip>
</case>
<case name="statistic_counter_4">
<x-axis display="byte">
<columns/>
</x-axis>
<legend/>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true">
<baseline>
<columns/>
</baseline>
</case>
</select>
<tooltip show-only-tooltip-fields="true">
<columns>
<select v1="!~blnCentralBaselineSelected!~">
<case v2="true"/>
</select>
</columns>
</tooltip>
</case>
you can see the there is 2 extra un needed output (statistic_counter_3/4, they are empty and should be calculated.)
sorry for the long mail, hope this is clear enough.
thanks for the help.
Kfir
|
|

September 9th, 2007, 02:19 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Sorry, but there's no way I'm going to read that amount of code. Try a cut-down example - you probably won't need to post it, because you'll be able to debug it yourself.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |