Link to home
Start Free TrialLog in
Avatar of vandy02
vandy02Flag for United States of America

asked on

JSP - XSL - Display in table

I have the following xsl file placed in the code section below that I would like to display in a html table.  

Thanks
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" />
  <xsl:template match="/">
    <html>
      <head>
        <title>JSTL XML Support -- Transforming</title>
      </head>
      <body bgcolor="#98AFC7">
        <h3>Transforming an XML Document using XML tags</h3>              
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>
 
  <xsl:template match="Author-Name">
    <!--printing the Last element-->
    LAST NAME :
    <xsl:apply-templates select="Last"/>
  </xsl:template>
 
  <xsl:template match="Last">
    <xsl:value-of select="text()" />
    <br />
  </xsl:template> 
 
  <xsl:template match="Book-Name" >
    <!--printing the BOOK-NAME element -->
    BOOK NAME : 
    <xsl:value-of select="text()" />
    <br />
  </xsl:template>
 
    <xsl:template match="Language" >
    <!--printing the BOOK-NAME element -->
    LANGUAGE :
    <xsl:value-of select="text()" />
    <hr />
  </xsl:template>
</xsl:stylesheet>

Open in new window

Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Use POI API
Avatar of vandy02

ASKER

I was thinking more on the line of an html table.
You are already creating HTML with the xsl file -- do you mean you need help producing the <table> tags in your xsl?
Avatar of vandy02

ASKER

Yes.
Well, you want something like
<table>
<xsl:for-each select="BookSection">
<tr><td><xsl:variable name="last" select="Author-Name/Last"/></td>
<td><xsl:variable name="last" select="Book-Name"/></td>
</tr>
</xsl:for-each>

Put all the values you want for the columns in.

I can't tell from your code what the outer section for each book xml is, but that's what you want as the for-each select value.

Then you don't use apply-templates or the xsl:template sections below.

Try this and see if it does something like what you want.
Avatar of vandy02

ASKER

Well this works, but the problem is it puts all the records in a row.  In other words, if I modify the xml within the jsp to be a little simpler I get the following from the below.

OUTPUT:
First1 Last1 First2 Last2

books.jsp
<%@ page contentType="text/xml" %><?xml version="1.0" encoding="UTF-8"?>
<Book>
  <Author>
    <First>
      First1
    </First>
    <Last>
      Last1
    </Last>
  </Author>
  <Author>
    <First>
      First2
    </First>
    <Last>
      Last2
    </Last>
  </Author>
</Book>

book.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" />
    <html>
      <head>
        <title>JSTL XML Support -- Transforming</title>
      </head>
      <body bgcolor="#98AFC7">
        <h3>Transforming an XML Document using XML tags</h3>
        <table>
            <xsl:for-each select="Author">
            <tr>
                <td><xsl:variable name="last" select="Last"/></td>
            </tr>
            </xsl:for-each>
        </table>
      </body>
    </html>
</xsl:stylesheet>
Great -- it sounds as if this was the solution.  Did you need help with something else?
Avatar of vandy02

ASKER

It put everything in one row when there exists two rows.  How can I put the data in two rows?
Avatar of vandy02

ASKER

Notiice the file there is two Authors.
ASKER CERTIFIED SOLUTION
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vandy02

ASKER

Fantastic!    Thanks!
You're welcome.  Good luck!