Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with <xsl:for-each> and Java Collections

Posted on 2007-04-10
7
Medium Priority
?
406 Views
Last Modified: 2013-11-18
Hello,
I am relatively new to XSL and I'm trying to use the <xsl:for-each> tag with my Java Collection to generate a table.
The problem is that although I am generating the correct number of table rows (so it correctly acknowledges the size of the list), none of the columns are being populated with the data from each list element, except the very last one which is a generic toString() output.
Thank you for any help you can provide!

Here is what I wrote in the *.xsl file:

<table>
  <xsl:for-each select="myList/item">
    <tr>
      <td><xsl:value-of select="name"/></td>
      <td><xsl:value-of select="age"/></td>
      <td><xsl:value-of select="."/></td>
    </tr>
  </xsl:for-each>
</table>

This is what the Java code looks like in my Java Struts Form class:

public class MyForm extends ActionForm {
  .....
  List myList;

  public MyForm() {
    myList = new ArrayList();
    myList.add(new Person("Bob", 30));
  }

  public List getMyList() {
    return myList;
  }
  .....
}

Person is defined as follows:

public class Person {
  String name;
  int age;

  public Person(String name, int age) { this.name = name; this.age = age; }
  public String getName() { return name; }
  public int getAge() { return age; }
  public String toString() { return name + ", " + age; }
}
0
Comment
Question by:dongjinkim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18883405
Try making Person bean-compliant. No-arg ctor, private fields, public setters
0
 
LVL 3

Author Comment

by:dongjinkim
ID: 18883501
Thanks for pointing that out.
I apologize for not showing the original code, but I assure you that the original code is bean compliant.
I tried to simplify the code in an effort to make it more readable, but it appears I left out some details.
0
 
LVL 92

Expert Comment

by:objects
ID: 18885947
try:

     <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@age"/></td>
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 11

Expert Comment

by:Vivek Thangaswamy
ID: 18896304
Hi

In this blog http://vivekthangaswamy.blogspot.com/2006/09/xml-and-xslt-transformation-using-cnet.html
you can find a source code attached, in that source code file you can find a XSLT in that XSLT thr are lots of Recursive functions and loops are done. It will be more helpfull for you. And you can find very good examples for for-each in XSL try out...
0
 
LVL 3

Author Comment

by:dongjinkim
ID: 18897916
I found out how to access the member methods of Person.
I had to change the XML as follow:

From:
  <xsl:for-each select="myList/item">
To:
  <xsl:for-each select="myList/Person">

I also had to override the HttpServlet's service method to handle the Person class.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 19501795
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question