Page 1 of 1
xpath expression
Posted: Thu Jan 12, 2006 2:14 pm
by lemonfreshmedia
Wondering how to achieve the following..
i have xx number of articles in my xml file
i'd like to divide them by 3
and get them to display in three (fairly) equal columns
so if i had
37
i'd like them to display like this
1. 13 25
... ... ...
12. 24. 36
with the remainder appearing on the last line
37.
sometimes it will be 30 sometimes it may be 35 or even 20.
Any help on this is much appreciated as I haven't been able to figure this one out.
Thanks
Posted: Thu Jan 12, 2006 2:58 pm
by timvw
A simple combination of count and division operator will get you there...
Could you be alittle more specific?
Posted: Thu Jan 12, 2006 4:45 pm
by lemonfreshmedia
i've found out how to get the remainder but how to apply it across columns is escaping me
Your tip worked, but i'm still at a loss.
Posted: Thu Jan 12, 2006 5:23 pm
by timvw
Here is a little sample:
if you have articles 1 to 6, you end up with a col1 having 1 and 4, col2 having 2 and 5, col3 having 3 and 6
(offcourse, for your problem you would need to do some magic with division instead

)
Code: Select all
<xsl:for-each select='articles/article'>
<xsl:if test="position() mod 3 = 0">
// first column
</xsl:if>
<xsl:if test="position() mod 3 = 1">
// second column
</xsl:if>
<xsl:if test="position() mod 3 = 2">
// third column
</xsl:if>
</xsl:for-each>
here's what I have so far
Posted: Fri Jan 13, 2006 4:27 pm
by lemonfreshmedia
for column 1
Code: Select all
[position() <= floor(last() div 3)]
for column 2
Code: Select all
[(position() > (floor(last() div 3)) ) and (position() <= ((floor(last() div 3)) * 2)) ]
for column 3
Code: Select all
[(position() > ((floor(last() div 3)) * 2)) and (position() <= ((floor(last() div 3)) * 3)) ]
only thing is when i try to get the remainder of number of articles divided by three the only case that indicates anything other than 1 is if the article number ends in '8'.
example
37 mod 3 = 1 one articled added to the first column
38 mod 3 = 2 one article added to the second column
39 mod 3 = 1 so if its tested to see for this number i get 1 again,
how can get this number to behave in a way that will indicate a perfectly formed row?
Re: here's what I have so far
Posted: Fri Jan 13, 2006 6:25 pm
by timvw
I snipped your code for columns.. You can make it a little more flexible (and save a bit of writing)
rowspercolumn = total / number of rows
for i := 0 to numbers of rows:
get rows where position between i * rowspercolumn and (i+1) * rowspercolumn
lemonfreshmedia wrote:
example
37 mod 3 = 1 one articled added to the first column
38 mod 3 = 2 one article added to the second column
39 mod 3 = 1 so if its tested to see for this number i get 1 again
Your example is wrong.
...
36 mod 3 = 0
37 mod 3 = 1
38 mod 3 = 2
39 mod 3 = 0
40 mod 3 = 1
41 mod 3 = 2
...
As you can see there are only 3 outcomes 0, 1 and 2. Now that you know this, you could use it to decide in which column the row belongs.
column1 | column2 | column3
-----------|------------|-----------
row 0 | row 1 | row 2
-----------|------------|------------
row 3 | row 4 | row 5
-----------|------------|------------
....
39 MOD 3 = 1
Posted: Fri Jan 13, 2006 6:40 pm
by lemonfreshmedia
i've executred that in my code and it returns 1.
unless i'm coding '39 mod 3' wrong .
the only way to return 0 after division is dividing a number by 0, no?
Thanks for all the help, still pluggin away at this if you had any more ideas.
Cheers.
Posted: Fri Jan 13, 2006 6:52 pm
by feyd
mod is the remainder after division.
Thanks,
Posted: Fri Jan 13, 2006 8:27 pm
by lemonfreshmedia
Although i'm still returning 1
when running 39 mod 3 I get a positive result when checking if (last()mod3) = 0
Thanks guys
I'd post my code but instead of using variables im running a php script to write csaes for each possibility.