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 October 29th, 2006, 10:29 PM
Authorized User
 
Join Date: Oct 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using XSLT to convert XML to a table ?

Hi all,
Suppose we have a XML file describe products as follow:
<?xml version="1.0" encoding="utf-8" ?>
<store>
    <product>
        <ImageUrl>product1.gif</ImageUrl>
        <Name>Screen</Name>
        <Price>12</Price>
    </product>
    <product>
        <ImageUrl>product2.gif</ImageUrl>
        <Name>Printer</Name>
        <Price>15</Price>
    </product>
    <product>
        <ImageUrl>product3.gif</ImageUrl>
        <Name>Printer</Name>
        <Price>20</Price>
    </product>
</store>
If we want to show 3 products per a line (and suppose we have many lines) as follow:
<table>
    <tr>
        <td>
            ...//product i info
        </td>
        <td>
            ...//product i+1 info
        </td>
        <td>
            ..//product i+2 info
        </td>
    </tr>
    <tr>
        <td>
            ...//product i+3 info
        </td>
        <td>
            ...//product i+4 info
        </td>
        <td>
            ..//product i+5 info
        </td>
    </tr>
..... //product i+n info
</table>

How can we obtain this by using XSLT ?
Thanks a lot !:)
 
Old October 30th, 2006, 04:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please see http://www.dpawson.co.uk/xsl/sect2/N7450.html#d9914e13

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 30th, 2006, 10:06 AM
Authorized User
 
Join Date: Oct 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks you very much !
But all products are showed three times .
I wonder if I made any mistakes ?.
Here is my code :
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="store">
<table bgcolor="#DDEEF7" WIDTH="100%" >
<xsl:for-each select="product">
<tr>
<xsl:for-each select=". | following-sibling :: product[position() < 3]">
<td>
....some code displaying product info here...
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
 
Old October 30th, 2006, 11:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, you made a mistake. The example I pointed you to starts

<xsl:for-each select="item[position() mod 5 = 1]">


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Subject: Convert XML using XSLT manoj1984 XSLT 6 May 30th, 2008 04:27 PM
Convert 1 big XML to multiple smaller XML victorcorey XSLT 1 March 20th, 2008 05:59 AM
Convert XML to XML template question bonekrusher XSLT 3 July 12th, 2007 07:47 AM
how to populate a table with xml data in xslt miccipynewbie XSLT 6 May 28th, 2007 07:29 AM
constructing a HTML table from xml data using xslt rameshnarayan XSLT 0 September 19th, 2005 06:53 AM





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