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 September 2nd, 2007, 09:02 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help on generating HTML table please!!

Hi everyone,

I am a beginner of XSLT and have a project on generating HTML table.Basically, I want to generate the HTML based on the other table specification in XML. The work will be very much akin to dataTable tag in JavaServer Faces.
For example:
I have XML specfication:
<table>
    <column>
        <text>Username</text>
        <text>Password</text>
    </column>
    <column>
        <inputTextField name="userName"/>
        <inputPasswordField name="password"/>
    </column>
</table>

and I want to generate the table as follow:

Username "<input type="text" name="userName"/>"
Password "<input type="password" name="password"/>"

The input should be rendered as normal text box and password field in browser.
The <text>,<inputTextField> and <inputPasswordField> can be handled by some "template match" in the stylesheet. The trickiest part is how to put the <tr> and <td> in the correct way to get the expected result.

I hope you can help me.

Thank you so much.









 
Old September 2nd, 2007, 10:14 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

For Starters your Table template in XSTL should look something like this:

Code:
    <xsl:template match="table">
        <table>

                <xsl:for-each select="column">
                    <tr>
                        <xsl:for-each select="text">
                            <td>
                                <xsl:apply-templates/>
                            </td>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>

        </table>
 
Old September 2nd, 2007, 10:19 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Creating tables that have the labels in one column and the data in an adjacent one is trickier than ones with a header row. I would apply-templates to table/column[1] and within that process each text element followed by each following-sibling::column/text where the position of each text element is the same as the current text position.

--

Joe (Microsoft MVP - XML)
 
Old September 2nd, 2007, 10:26 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Try something like this:

Code:
<xsl:template match="table">
    <xsl:variable name="cols" select="count(column[1]/*)" />
    <table>
        <xsl:for-each-group select="//column/*" group-adjacent="position() mod $cols">
            <tr>
            <xsl:for-each select="current-group()">
                <xsl:apply-templates select="." />
            </xsl:for-each>
            </tr>
        </xsl:for-each-group>
    </table>
</xsl:template>
/- Sam Judson : Wrox Technical Editor -/
 
Old September 2nd, 2007, 10:58 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

For a version one solution something like:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <table>
      <xsl:apply-templates select="table/column[1]"/>
    </table>
  </xsl:template>

  <xsl:template match="table/column">
    <xsl:for-each select="text">
      <xsl:variable name="position" select="position()"/>
      <tr>
        <td><xsl:value-of select="."/></td>
        <td><xsl:apply-templates select="../../column[2]/*[position() = $position]"/></td>
      </tr>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="inputTextField">TEXTBOX</xsl:template>

  <xsl:template match="inputPasswordField">PASSWORDBOX</xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old September 2nd, 2007, 12:35 PM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much,friends!

The version by joefawcett works for me, it does generate the table with label for one column and data in adjacent column.It can be used to dynamically generate HTML form with widget specified in XML. Also thank you samjudson and bonekrusher for your solution.

It is great to participate in the forum.

Regards,






Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating multiple HTML files dunpeal ASP.NET 1.0 and 1.1 Professional 6 August 24th, 2006 07:16 AM
generating a table w. xslt, newbie problem themuffinman_swe XSLT 5 November 26th, 2005 01:05 PM
Generating a 3 Column Table garethdown44 XSLT 4 June 14th, 2005 01:22 PM
Generating html tags dynamically sachin lad Servlets 1 April 26th, 2005 05:35 PM
Client Side JavaScript Generating HTML Files interrupt Javascript How-To 1 January 14th, 2005 11:36 AM





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