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

Posted on 2007-04-10
Medium Priority
Last Modified: 2013-11-18
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:

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

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; }
Question by:dongjinkim
LVL 86

Expert Comment

ID: 18883405
Try making Person bean-compliant. No-arg ctor, private fields, public setters

Author Comment

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.
LVL 92

Expert Comment

ID: 18885947

     <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@age"/></td>
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

LVL 11

Expert Comment

by:Vivek Thangaswamy
ID: 18896304

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

Author Comment

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

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

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

Accepted Solution

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

EE Admin

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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

587 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