xpath expression
Moderator: General Moderators
-
lemonfreshmedia
- Forum Commoner
- Posts: 26
- Joined: Fri Dec 02, 2005 7:14 pm
xpath expression
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
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
-
lemonfreshmedia
- Forum Commoner
- Posts: 26
- Joined: Fri Dec 02, 2005 7:14 pm
Could you be alittle more specific?
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.
Your tip worked, but i'm still at a loss.
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
)
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>-
lemonfreshmedia
- Forum Commoner
- Posts: 26
- Joined: Fri Dec 02, 2005 7:14 pm
here's what I have so far
for column 1
for column 2
for column 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?
Code: Select all
[position() <= floor(last() div 3)]Code: Select all
[(position() > (floor(last() div 3)) ) and (position() <= ((floor(last() div 3)) * 2)) ]Code: Select all
[(position() > ((floor(last() div 3)) * 2)) and (position() <= ((floor(last() div 3)) * 3)) ]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
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
...
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
-----------|------------|------------
....
rowspercolumn = total / number of rows
for i := 0 to numbers of rows:
get rows where position between i * rowspercolumn and (i+1) * rowspercolumn
Your example is wrong.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
...
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
-----------|------------|------------
....
-
lemonfreshmedia
- Forum Commoner
- Posts: 26
- Joined: Fri Dec 02, 2005 7:14 pm
39 MOD 3 = 1
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.
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.
-
lemonfreshmedia
- Forum Commoner
- Posts: 26
- Joined: Fri Dec 02, 2005 7:14 pm
Thanks,
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.
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.