Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 July 29th, 2008, 01:05 PM
Registered User
 
Join Date: Jul 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Columns

Hi,

I've been trying for awhile now to figure out how to make a dynamic table with dynamic columns. I've found tons of examples on the web but I cant seem to get any of them to work properly. So I figured I must be doing something wrong.

Here is my sample XML:

Catalog.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
  <item id="1">
    <item-title>Test</item-title>
    <item-price>#36;9.99</item-price>
    <item-picture>/images/catalog/picture1.jpg</item-picture>
  </item>
  <item id="2">
    <item-title>Test 2</item-title>
    <item-price>#36;9.99</item-price>
    <item-picture>/images/catalog/picture2.jpg</item-picture>
  </item>
  <item id="3">
    <item-title>Test 3</item-title>
    <item-price>#36;9.99</item-price>
    <item-picture>/images/catalog/picture1.jpg</item-picture>
  </item>
  <item id="4">
    <item-title>Test 4</item-title>
    <item-price>#36;9.99</item-price>
    <item-picture>/images/catalog/picture2.jpg</item-picture>
  </item>
  <item id="5">
    <item-title>Test 5</item-title>
    <item-price>#36;9.99</item-price>
    <item-picture>/images/catalog/picture1.jpg</item-picture>
  </item>
  <item id="6">
    <item-title>Test 6</item-title>
    <item-price>#36;9.99</item-price>
    <item-picture>/images/catalog/picture2.jpg</item-picture>
  </item>
  <item id="7">
    <item-title>Test 7</item-title>
    <item-price>#36;9.99</item-price>
    <item-picture>/images/catalog/picture1.jpg</item-picture>
  </item>
  <item id="8">
    <item-title>Test 8</item-title>
    <item-price>#36;9.99</item-price>
    <item-picture>/images/catalog/picture2.jpg</item-picture>
  </item>
</catalog>
catalog_display.xsl
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="1.0">
<xsl:output method="html" />
<xsl:variable name="columns" select="3" />
<xsl:template match="catalog">
    <table border="1">
        <xsl:for-each select="item[position() mod $columns = 1]">
            <tr>
                <xsl:for-each select=".|following-sibling::item[position() &gt; 3]">           
                    <td>
                        <xsl:value-of select="." />
                    </td>
                </xsl:for-each>
            </tr>            
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>
Basically suppose to create a new row every 3 items. But I can figure out where it actually tells it to create a new table row. Anyway, if someone can please help me or point me in the right direction I'll greatly appreciate it.
 
Old July 29th, 2008, 01:21 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your [position() &gt; 3] should clearly be [position() &lt; 3].

But I'm not sure I understand your question. The literal result element <tr>...</tr> causes a new row to be created, and the surrounding <xsl:for-each> causes it to be done once for every <item> element that satisfies the condition [position() mod $columns = 1] - that is, for the 1st, 4th, 7th etc item element.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old July 29th, 2008, 01:25 PM
Registered User
 
Join Date: Jul 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You know I've been staring at the damn code all day and that never dawned on me to change &gt; to &lt;. Thanks for the fresh pair of eyes on my code.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic columns in data controls p330418 ASP.NET 2.0 Professional 0 April 10th, 2008 11:10 PM
Dynamic creation of Columns in datagrid p330418 ASP.NET 2.0 Basics 0 March 29th, 2008 07:33 AM
Dynamic Columns & Dynamic Grouping ??? nileshgambhava BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 1 March 24th, 2008 07:58 AM
Dynamic columns pankaj_daga Crystal Reports 1 August 30th, 2007 11:07 AM
Dynamic Columns & Dynamic Grouping ??? nileshgambhava SQL Server 2005 0 December 12th, 2006 12:45 AM





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