xsl-list
[Top] [All Lists]

Re: Splitting data into smaller groups for HTML output.

2005-10-31 11:34:42
Mike,

The one thing you don't say is by what rule you would like the splitting to occur (you only say "into two or more groups").

If you wanted to split into exactly two groups, you could do:

<xsl:template match="sources">
  <div>
    <xsl:apply-templates
      select="source[position() &lt;= (last() div 2)"/>
  </div>
  <div>
    <xsl:apply-templates
      select="source[position() &gt; (last() div 2)"/>
  </div>
</xsl:template>

Notice if you have an odd number of source element children, the first group will be short one item.

There are other methods available for grouping them differently and even according to data-driven criteria, for example if you need to determine dynamically how many groups you want.

Cheers,
Wendell

At 01:34 PM 10/31/2005, you wrote:
Hello,

I'm fairly new to XSL and despite picking things up quickly I've hit a
problem with a template I'm trying to create.

I have an XML file which is structured like this:

<sources>
    <source>
        <title>Title 1</title>
        <url>http://url1.com/</url>
    </source>
    <source>
        <title>Title 2</title>
        <url>http://url2.com/</url>
    </source>
    <source>
        <title>Title 3</title>
        <url>http://url3.com/</url>
    </source>
    <source>
        <title>Title 4</title>
        <url>http://url3.com/</url>
    </source>
</sources>

...and so on. The XML file is dynamically generated by PHP and there
are usually around 10 - 25 <source> nodes and only ever one <sources>
node.

What I've been trying to do is use a template to output these
<sources> in to an HTML file, but and here's the rub - splitting the
<sources> into two or more groups within the HTML. It'd be easier to
show an example...

<div>
  <a href="http://url1.com/";>Title 1</a><br />
  <a href="http://url2.com/";>Title 2</a><br />
</div>
<div>
  <a href="http://url3.com/";>Title 3</a><br />
  <a href="http://url4.com/";>Title 4</a><br />
</div>

The best I can achieve, using <xsl:for-each> is:

<div>
  <a href="http://url1.com/";>Title 1</a><br />
  <a href="http://url2.com/";>Title 2</a><br />
  <a href="http://url3.com/";>Title 3</a><br />
  <a href="http://url4.com/";>Title 4</a><br />
</div>


Doing something like this is pretty straightforward using PHP, with
counter variables and what-not. I realise that XSL works differently
in this respect and was hoping that someone might have some advice for
me on how I can achieve the desired results.


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--