[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to create tables from this...?

Subject: Re: How to create tables from this...?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 23 Apr 2002 17:22:37 +0100
create table in xslt
Hi Emma,

> So, I want to create tables, one table for each set of elements that
> are not paragraphs. These sets are separated by paragraphs and they
> contain paragraphs that should be included in the table... Any
> suggestions of how to do this?

I think that the best way to approach this kind of transformation in
XSLT 1.0 is to use a step-by-step method, where you step through the
paragraphs and things that aren't paragraphs one at a time and decide
what to do with them on a case-by-case basis.

You need one template in 'group' mode that deals with the paragraphs
by copying the paragraph and moving onto the next node, whatever it
is:

<xsl:template match="paragraph" mode="group">
  <xsl:copy-of select="." />
  <xsl:apply-templates select="following-sibling::*[1]"
                       mode="group" />
</xsl:template>

and another template in 'group' mode that matches other things and
creates a table:

<xsl:template match="*" mode="group">
  <table>
    ...
  </table>
  ...
</xsl:template>

After the table, you want the results of applying templates to the
next paragraph element, in 'group' mode:

<xsl:template match="*" mode="group">
  <table>
    ...
  </table>
  <xsl:apply-templates select="following-sibling::paragraph[1]"
                       mode="group" />
</xsl:template>

For the content inside the table, you need to apply templates to the
element itself in 'item' mode:

<xsl:template match="*" mode="group">
  <table>
    <xsl:apply-templates select="." mode="item" />
  </table>
  <xsl:apply-templates select="following-sibling::paragraph[1]"
                       mode="group" />
</xsl:template>

Then you need a template in 'item' mode that steps through the
elements one by one copying them (or whatever you need to do to fill
the table), and moving on to the next item, as long as it isn't a
paragraph:

<xsl:template match="*" mode="item">
  <xsl:copy-of select="." />
  <xsl:apply-templates mode="item"
    select="following-sibling::*[1][not(self::paragraph)]" />
</xsl:template>

To start the whole process off, you need to apply templates to the
first child of the side-effects element in group mode:

<xsl:template match="side-effects">
  <xsl:apply-templates select="*[1]" mode="group" />
</xsl:template>

----

As with all grouping problems, this is easier in XSLT 2.0. Here, you
can use the group-adjacent type of xsl:for-each-group to basically
toggle between paragraphs and not-paragraphs, like this:

<xsl:template match="side-effects">
  <xsl:for-each-group select="*"
                      group-adjacent="self::paragraph">
    <xsl:choose>
      <xsl:when test="self::paragraph">
        <!-- group of paragraphs; copy them -->
        <xsl:copy-of select="current-group()" />
      </xsl:when>
      <xsl:otherwise>
        <!-- group of other elements -->
        <table>
          <xsl:copy-of select="current-group()" />
        </table>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.