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 July 1st, 2005, 09:27 AM
Registered User
 
Join Date: Jul 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT and tables

Hello, I have an issue with transforming my xml file into html:

My xml has the following structure:

<COLUMN>
  <NAME>Labe1</NAME>
  <VALUE>Value1</VALUE>
  <VALUE>Value2</VALUE>
  <VALUE>Value3</VALUE>
</COLUMN>

<COLUMN>
  <NAME>Labe2</NAME>
  <VALUE>Value11</VALUE>
  <VALUE>Value21</VALUE>
  <VALUE>Value31</VALUE>
</COLUMN>

I want to build an html table with COLUMN/NAME as a row headers and
COLUMN/VALUE as rows down from the header. Thus the table will have two columns with 'Label1' and 'Label2' headers with 3 rows each and I have serious problems understanding how to build such a table.

please help!


 
Old July 3rd, 2005, 03:36 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Assuming you don't know how many columns there are, otherwise there are simpler ways then this XML:
Code:
<TABLE>
  <COLUMN>
    <NAME>Labe1</NAME>
    <VALUE>Value1</VALUE>
    <VALUE>Value2</VALUE>
    <VALUE>Value3</VALUE>
  </COLUMN>
  <COLUMN>
    <NAME>Labe2</NAME>
    <VALUE>Value11</VALUE>
    <VALUE>Value21</VALUE>
    <VALUE>Value31</VALUE>
  </COLUMN>
</TABLE>
with the XSLT:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <table style="border: 2px solid #c0c0c0">
          <thead>
            <tr>
              <xsl:for-each select="*/COLUMN/NAME">
                <th>
                  <xsl:value-of select="." />
                </th>
              </xsl:for-each>
            </tr>
          </thead>
          <tbody>
            <xsl:for-each select="*/COLUMN[1]/VALUE">
              <tr>
                <xsl:variable name="position" select="position()" />
                <xsl:apply-templates select="/*/COLUMN/VALUE[$position]" />
              </tr>
            </xsl:for-each>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="VALUE">
    <td>
      <xsl:value-of select="." />
    </td>
  </xsl:template>
</xsl:stylesheet>
should get you started.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Xslt and wml tables display arunagottimukkala XSLT 4 October 24th, 2007 03:39 AM
XSLT Row replication in Tables jsuen XSLT 1 August 7th, 2006 11:07 AM
Two HTML Tables and Two XSLT with Two XML? gman44 XSLT 1 March 1st, 2006 04:53 AM
regarding tables creation in xslt barsha XSLT 0 September 28th, 2005 07:50 AM
xslt looping with tables... lukemedway_uk XSLT 16 December 24th, 2003 12:24 PM





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