Solved

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

Posted on 2007-04-10
7
393 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
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now