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 December 9th, 2003, 01:08 AM
Registered User
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to loop through a HashMap in XSL?

Hi All,
I need to pass a set of dynamic filter conditions to XSL file from Java program. Hence, I created a HashMap of filter conditions and passed them from Java program to XSL file. How do I loop through that HashMap in XSL to retrieve the key-value pairs?
Thanks and Regards,
Sunil.
 
Old December 9th, 2003, 01:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by sunilnk
 Hi All,
I need to pass a set of dynamic filter conditions to XSL file from Java program. Hence, I created a HashMap of filter conditions and passed them from Java program to XSL file. How do I loop through that HashMap in XSL to retrieve the key-value pairs?
Thanks and Regards,
Sunil.
What you can pass to XSLT stylesheet are so called stylesheet parameters. For example, in the command line you can write:
Code:
saxon source.xml sheet.xsl param1=value1 param2=value2 ...
However, it's impossible to dynamically change XPath expressions (as I understand, you want to dynamically create predicates) in stylesheets. Post your stylesheet source and say what you want to do exactly, and we'll try to find a solution.

Regards,
Armen
 
Old December 9th, 2003, 02:23 AM
Registered User
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here are the details:
My main requirement is to pass a set of filter parameters to the XSL dynamically. I am using the XALAN processor.
I am trying to pass the HashMap of filters from Java program as follows:

            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource("coltest.xsl"));

            HashMap paramMap = new HashMap();
            paramMap.put("country", "EU");
            paramMap.put("title", "Mac");
            transformer.setParameter("parameters", paramMap);
            transformer.transform(new StreamSource("catalog.xml"),new StreamResult(System.out));

The XSL file is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xalan="http://xml.apache.org/xalan"
    xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java">

<xsl:output method="html" />
<xsl:param name="parameters" select="java:java.util.HashMap.new" />

<xsl:template match="/">
    <html>
        <body>
            <table border="2" bgcolor="yellow">
                <tr>
                    <th>Title</th>
                    <th>Artist</th>
                <xsl:apply-templates select="catalog/cd">
                    <xsl:with-param name="parameters" select="$parameters" />
                </xsl:apply-templates>
                </tr>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="cd">



<xsl:variable name="keys" select="java:keySet($parameters)" />

<xsl:for-each select="$keys">

</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

Let me know if this feasible using XSL.
If not, what is a viable solution to pass filter conditions dynamically - both tag name and value for that being dynamic.

Thanks and Regards,
Sunil.
 
Old December 10th, 2003, 01:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Sorry for the late response: I was busy...

So, Xalan (2.5.2, for example) gives you an opportunity to write your own extensions.
Read first this: http://xml.apache.org/xalan-j/extensions.html.
You can manipulate the HashTable like this: http://xml.apache.org/xalan-j/extensions.html#ex-java .
This example will help you to accomplish your work. The drawback of your stylesheet is that it depends on the concrete XSLT processor.

Regards,
Armen
 
Old December 15th, 2003, 05:28 AM
Registered User
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great... it worked! Thanks for the help.

Regards,
Sunil.

 
Old April 10th, 2017, 12:54 AM
Registered User
 
Join Date: Apr 2017
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question read values from hashmap in xslt

i tried reading setting hashmap at jav end and use that map in my XSLT and generate dynamic values. But it didnot work using saxon.
 
Old April 10th, 2017, 04:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

swarna$, please don't add a completely new question to a thread that closed over 13 years ago. Ask a new question. And please say exactly what you were doing and exactly how it failed.

Also, unless your question is specifically about something you read in a Wrox book, the best place to get answers to XSLT coding questions these days is StackOverflow.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
help: how can I break from xsl for-each loop ? back2grave XSLT 5 May 31st, 2012 12:03 PM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 10:41 AM
xsl:for-each loop Tomi XSLT 3 September 7th, 2006 05:35 AM
Loop in XSL NEO1976 XSLT 31 September 5th, 2006 03:39 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.