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

November 16th, 2012, 12:02 PM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 16th, 2012, 09:21 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
|
|
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:
|
|
|

November 17th, 2012, 06:34 AM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by bonekrusher
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
|
|

November 17th, 2012, 08:42 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
|
|
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:
|
|
|

November 17th, 2012, 09:35 AM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by bonekrusher
|
I think we are at cross purposes here. I want to do this in a Transformation XSLT template document using xslt / xpath.
|
|

November 17th, 2012, 10:38 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
|
|
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:
|
|
|

November 17th, 2012, 10:43 AM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by bonekrusher
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?
|
|

November 19th, 2012, 09:56 AM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
Hi, with a few tweaks here (Java) and there (XSLT) I now have this working. Thanks.
--
William
|
|
 |