Solved

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

Posted on 2007-04-10
7
394 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 40
XML & .net 5 40
Windows and other UI names - The Proper names for the Various elements! 1 55
MySQL  on Tomcat 8 23
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

930 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

14 Experts available now in Live!

Get 1:1 Help Now