Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 17th, 2006, 01:06 PM
Authorized User
 
Join Date: May 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl formatting table

Hi I have an xml

<Fields>
 <Field type="Separator" pos="start" />
 <Field type="1" />
 <Field type="2" />
 <Field type="3" />
 <Field type="Separator" pos="start" />
 <Field type="4" />
 <Field type="3" />
 <Field type="Separator" pos="start" />
 <Field type="2" />
 <Field type="3" />
...
</Fields>

I need an xsl that would put the field with type other than Separator into separate tables like

<table>
 <tr>
   <td>1</td>
   <td>2</td>
   <td>3</td>
 </tr>
</table>
<table>
   <td>4</td>
   <td>3</td>
   <td></td>
</table>
<table>
   <td>2</td>
   <td>3</td>
   <td></td>
</table>

Please help thanks

Cris+

 
Old May 18th, 2006, 01:59 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0, use

<xsl:for-each-group select="Field" group-starting-with="Field[@type='Separator']">

For 1.0 it's more difficult but there are well-known solutions: try a search for "XSLT positional grouping".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 18th, 2006, 12:47 PM
Authorized User
 
Join Date: May 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi I almost got it, I have an xml

<?xml version="1.0"?>
<Page>
  <Fields>
    <Field type="HI" label="1"></Field>
    <Field type="HI" label="2"></Field>
    <Field type="HI" label="3"></Field>
    <Field type="YES" label="YES"></Field>
    <Field type="HI" label="4"></Field>
    <Field type="HI" label="5"></Field>
  </Fields>
</Page>



and xsl



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove">
    <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"></xsl:output>
    <xsl:template match="/Page">
        <table>
            <xsl:apply-templates select="Fields" />
        </table>
    </xsl:template>
    <xsl:template match="Fields">
        <xsl:apply-templates select="Field" />
    </xsl:template>
    <xsl:template match="Field">
        <xsl:choose>
            <xsl:when test="@type='YES'">
                <xsl:value-of select="@type" />
            </xsl:when>
            <xsl:when test="((@type='HI') and not(preceding-sibling::Field[1]/@type='HI'))">
                <table>
                    <tr>
                        <td>
                            <xsl:value-of select="@label" />
                        </td>
                        <xsl:call-template name="generate-table-elements">
                            <xsl:with-param name="list" select="following-sibling::Field/@type" />
                        </xsl:call-template>
                    </tr>
                </table>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="generate-table-elements">
        <xsl:param name="list" />
        <xsl:if test="$list[1]='HI'">
            <td>
                <xsl:value-of select="@label" />
            </td>
            <xsl:call-template name="generate-table-elements">
                <xsl:with-param name="list" select="$list[position() > 1]" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>


in which the output goes


<table xmlns:asp="remove">
<table>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</table>YES<table>
<tr>
<td>4</td>
<td>4</td>
</tr>
</table>
</table>


but I need it to be

<table xmlns:asp="remove">
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>YES<table>
<tr>
<td>4</td>
<td>5</td>
</tr>
</table>
</table>

what am I doing wrong here? Thanks

 
Old May 18th, 2006, 01:20 PM
Authorized User
 
Join Date: May 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dont bother I got it, thanks.






Similar Threads
Thread Thread Starter Forum Replies Last Post
very urgent:cals table to indesign table using xsl franklinclinton XSLT 1 December 16th, 2009 03:48 PM
3D Array Looping - Formatting in table jordan23 XSLT 3 April 30th, 2007 11:57 AM
table formatting in asp.net balesh ASP.NET 1.0 and 1.1 Basics 1 June 8th, 2006 07:51 AM
Formatting dates in XSL francislang XSLT 4 September 3rd, 2004 08:17 AM
stupid table formatting question badgolfer ASP.NET 1.0 and 1.1 Basics 1 July 20th, 2004 07:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.