Subject: XSLT and tables
Posted By: surgeon Post Date: 7/1/2005 9:27:02 AM
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!


Reply By: joefawcett Reply Date: 7/3/2005 3:36:44 AM
Assuming you don't know how many columns there are, otherwise there are simpler ways then this XML:
<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:
<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)

Go to topic 31714

Return to index page 518
Return to index page 517
Return to index page 516
Return to index page 515
Return to index page 514
Return to index page 513
Return to index page 512
Return to index page 511
Return to index page 510
Return to index page 509