Solved

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

Posted on 2007-04-10
7
396 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 67
javap bin 2 34
arguments to jar 5 26
Android development question 2 34
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

785 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