Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2012, 12:02 PM
Authorized User
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default Iterating a Java HashMap in an XSLT file.

Hello,

The first thing I should ask is is this possible?

I am using Extensions (Saxon-B) and I have a HashMap
Code:
Map < String, Double > myList = new HashMap< String, Double >() ;
...
that I want to iterate over in an XSL template to create a series of attributes with values, with the key being the attribute name and the value being it's value.

Your help would be appreciated.
--
William
 
Old November 16th, 2012, 09:21 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Yes, this is possible. Your HashMap is static, so you can create static java method that excepts the "key" as a parameter and returns the "value".

Code:
public static Map < String, Double > myList = new HashMap< String, Double >() ;
public static String getValue(String key){

 // do the work

}
The Following User Says Thank You to bonekrusher For This Useful Post:
WilliamYou (November 17th, 2012)
 
Old November 17th, 2012, 06:34 AM
Authorized User
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by bonekrusher View Post
Yes, this is possible. Your HashMap is static, so you can create static java method that excepts the "key" as a parameter and returns the "value".

Code:
public static Map < String, Double > myList = new HashMap< String, Double >() ;
public static String getValue(String key){

 // do the work

}
Hello,

Thanks for that but sadly this approach does not work for me as I do not know the key values. The values are known but only as an attributeGroup in the Schema, so I have no way of knowing what or how many values are included in the map.

For what it's worth I could have got the same effect by importing java.util.Map and using it's get() method, but I need a solution that allows me to iterate the map so I can extract both the key's value (i.e. name) and the Double value assigned to it.

--
William
 
Old November 17th, 2012, 08:42 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

See http://stackoverflow.com/questions/1...hrough-hashmap on how to iterate over a hashmap for both keys and values...
The Following User Says Thank You to bonekrusher For This Useful Post:
WilliamYou (November 17th, 2012)
 
Old November 17th, 2012, 09:35 AM
Authorized User
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by bonekrusher View Post
See http://stackoverflow.com/questions/1...hrough-hashmap on how to iterate over a hashmap for both keys and values...
I think we are at cross purposes here. I want to do this in a Transformation XSLT template document using xslt / xpath.
 
Old November 17th, 2012, 10:38 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi William,

You can when you use extensions. If you are using Saxon, which is a XSLT Processor written in Java, you can write java extensions.
The Following User Says Thank You to bonekrusher For This Useful Post:
WilliamYou (November 17th, 2012)
 
Old November 17th, 2012, 10:43 AM
Authorized User
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by bonekrusher View Post
Hi William,

You can when you use extensions. If you are using Saxon, which is a XSLT Processor written in Java, you can write java extensions.
Thanks bonekrusher.

This exactly what I am doing as the original post states. So are you saying with the correct imports (via namespaces) I can do this in XSLT just like in Java?
 
Old November 19th, 2012, 09:56 AM
Authorized User
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Hi, with a few tweaks here (Java) and there (XSLT) I now have this working. Thanks.

--
William





Similar Threads
Thread Thread Starter Forum Replies Last Post
Page 345 - iterating through an xml file markhh ASP.NET 4 General Discussion 2 November 25th, 2011 11:49 AM
Question on Hashmap alothman BOOK: Professional C++ 1 September 3rd, 2006 02:13 AM
XSLT with Java Hind XSLT 1 May 24th, 2006 04:18 AM
HashMap mudit_12 Intro Programming 0 March 3rd, 2005 12:46 AM
HashMap freezotic BOOK: Beginning Java 2 6 March 26th, 2004 04:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.