JSP - XSL - Display in table

vandy02
vandy02 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Use POI API

Author

Commented:
I was thinking more on the line of an html table.
Top Expert 2007

Commented:
You are already creating HTML with the xsl file -- do you mean you need help producing the <table> tags in your xsl?
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Author

Commented:
Yes.
Top Expert 2007

Commented:
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.

Author

Commented:
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>
Top Expert 2007

Commented:
Great -- it sounds as if this was the solution.  Did you need help with something else?

Author

Commented:
It put everything in one row when there exists two rows.  How can I put the data in two rows?

Author

Commented:
Notiice the file there is two Authors.
Top Expert 2007
Commented:
The xsl:for-each logic is pretty clear.  Identify the level at which you want the data to loop, and run the for-each around it.  Is it possible that you are not posting the xsl you are actually using?  Because the XSL you've posted won't run in my tests.

This one works:

<?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>
        <table>
            <xsl:for-each select="Book/Author">
            <tr>
                <td><xsl:variable name="last" select="Last"/><xsl:value-of select="$last"/></td>
            </tr>
            </xsl:for-each>
        </table>
      </body>
    </html>
</xsl:template>
</xsl:stylesheet>

Author

Commented:
Fantastic!    Thanks!
Top Expert 2007

Commented:
You're welcome.  Good luck!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial