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 13th, 2004, 11:44 AM
Registered User
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Breaking the XML data into rows

I have the XML as

<Information>
    <Headlines>
    <Info>
        <Sym>BEAS</Sym>
        <Sym>BMC</Sym>
        <Sym>CA</Sym>
        <Sym>MERQ</Sym>
        <Sym>MSFT</Sym>
        <Sym>NTIQ</Sym>
        <Sym>NOVL</Sym>
        <Sym>ORCL</Sym>
        <Sym>PSFT</Sym>
        <Sym>QSFT</Sym>
                <Sym>ACDF</Sym>
                <Sym>GHT</Sym>
                <Sym>QIOU</Sym>
                <Sym>QREW</Sym>
                <Sym>NBHJ</Sym>
    </Info>
    </Headlines>
</Information>

Now I want to

* loop through the <Info>/<Sym>
* see how many records I have
* display the first 3 records in the first row of a table
* display the next 3 records in the second row of the table
* display the next 3 records in the third row of the table and so on

Is it possible to just use XSL to create the table and display the records in the fashion above?
 
Old May 13th, 2004, 12:00 PM
Authorized User
 
Join Date: May 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use the <xsl:if test="position()" > construct. position() gives you
the number of the element. Then apply different templates depending
on the position. Each <xsl:template match="MyTable1..table2.. etc">
would handle a separate table.

SS
 
Old May 13th, 2004, 12:32 PM
Registered User
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I wouldn't want to use different templates. Is there any other way I can do this? How do I use the position()?

 
Old May 13th, 2004, 12:37 PM
Authorized User
 
Join Date: May 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I can't see any other way..this could be a limitation in my know-how.
Regrading position, you would use it with <xsl:choose> and say
position() <=3 position() <= 6 etc..

SS
 
Old May 13th, 2004, 03:11 PM
Registered User
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried the following, but it displays all the records in 1 row one after the other rather than 3 records per row. Can someone help me out here?

<xsl:template match="/">

    <table border="0" cellpadding="0" cellspacing="0">

    <xsl:for-each select="Information/Headlines/Info/Sym">
        <xsl:value-of select="."/>
        <xsl:if test="Symbol[position() = 3]"><br/></xsl:if>
    </xsl:for-each>

    </table>

    <br/>

    The Total Number of Records is :
    <xsl:value-of select="count(Information/Headlines/Info/Sym)"></xsl:value-of>

    </xsl:template>

 
Old May 28th, 2004, 02:31 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

I am doing something very similar, and having a very similar problem with XSLT. (I'm doing 2 columns, not 3)

My XML source is:
Code:
    <project-news>
        <news-item level="1">
            <item-title>FLASHWAVE 4100 R4.1 Readies for PR1</item-title>
            <item-text>Work continues to bring the 4100 Project (4.1) toward PR1</item-text>
            <item-text>The NMIS has been updated and is Draft ready for PR1</item-text>
        </news-item>
    </project-news>
The XSLT I'm working with is:
Code:
    
    <xsl:template match="news-item" mode="new">
        <xsl:variable name="num-items" select="count(item-text)"/>
        <table width="80%">
            <tr>
                <th>
                    <xsl:if test="$num-items > 1">
                        <xsl:attribute name="colspan">2</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="item-title"/>
                </th>
            </tr>
            <xsl:for-each select="item-text">
                <xsl:if  test="position() mod 2 != 0"><tr></xsl:if>
                <td><xsl:value-of select="."/></td>
                <xsl:if  test="position() mod 2 = 0 or position() = last()"></tr></xsl:if>
            </xsl:for-each>
        </table>
    </xsl:template>
Obviously, this isn't going to work as the LREs: <tr> and </tr> are not matching correctly up in the document object, the nodes are not complete.

Michael Kay says (paraphrased from his book)
Quote:
quote:It is tempting to think of this as a sequence of a <tr> tag, some text and a </tr> closing tag.
However, this is not a true picture of what is going on, and it is best not to think about it this way, because otherwise you will start wondering, for example, how to delay writing the end tag untilsome condition is encountered in the input.
Yup, that's what I'm doing. Unfortunatly, he doesn't go on to offer a solution.

So I am open to solutions.



------------------------
GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF&nbsp;&nbsp;ACE5 CA93 7AD5 D8E3 A876

Michael Hare





Similar Threads
Thread Thread Starter Forum Replies Last Post
Default Rows Based Upon Number of XML Attributes. Mr.T Infopath 0 November 5th, 2007 07:29 PM
Counting Number of Rows Between Data Range eusanpe Excel VBA 6 September 21st, 2006 07:17 AM
Data Report: Columns Against Rows bemular VB How-To 0 January 6th, 2005 12:52 AM
Determine the number of data rows in a sheet ashu_gupta75 Excel VBA 1 July 31st, 2004 03:10 PM





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