Subject: Re: [xsl] How to create tables from this...? From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Tue, 23 Apr 2002 17:22:37 +0100 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] How to create tables from, Michael Kay | Thread | RE: [xsl] How to create tables from, McNally, David |
RE: [xsl] How to create tables from, Michael Kay | Date | [xsl] structuring templates to gene, Andrew Welch |
Month |