Search

Sorry for the poor title. I have a collection of elements, and I want to split them into three “collections” of equal size.

I have the following XML: pastebin

I’d like to generate the following HTML: pastebin

So the thing is that I want the first third in the first list item (with it’s elements as a sub-list), and the second in the second, etc.

The solution I have now works, but is far from elegant: pastebin

The difficult part was considering the situation when the number of xml elements was not divisible by three. Eg. if I have 13 elements, then I want it distributed between the list items like so:

  • 1st list item: 4 sub items
  • 2nd list item: 3 sub items
  • 3rd list item: 3 sub items

Is there a way of doing the samething as my solution does except more elegantly? Thank you in advance.

Is there a way of doing the samething as my solution does except more elegantly?

I don’t know about more elegantly. I’ve had the same requirement before and achieved it in a similar way, just slightly different code.

http://gist.github.com/384074

But I’m sure there must be a more elegant solution using preceding/following-sibling axes, I just haven’t found it yet :-)

For what it’s worth, I can barely explain the code I wrote; I forget quite why I’m doing the subractions I’m doing. I do recall there being a lot of trial and error while testing it, but hopefully it is of use.

I had to do this once too. Can’t remember why, when, or how. So… that’s no help at all. But if it comes to me I’ll be sure to post back here :)

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details