xsl-list
[Top] [All Lists]

RE: Passing a Map as parameter to XSLT.

2006-03-04 04:07:08
Try doing

<xsl:template match="/">
  <xsl:copy-of select="$XSLTParameter"/>

Then we can see whether the problem is in passing the document to your
transformation, or in your use of keys to navigate within the document once
it has been passed in. Divide and conquer.

In JAXP, the kinds of object you can pass in as the second argument of
setParameter() depend on the XSLT processor you are using. So please let us
know the product and product version.

(Actually, in Saxon, it's even more complicated than that. A DOM document
will be recognized as a parameter value if the DOM support module
saxon8-dom.jar is on your classpath, and it won't be recognized otherwise.
DOM support was taken out of the mainstream JAR file because of the
difficulties with DOM incompatibility between JDK 1.4 and JDK 1.5)

Michael Kay
http://www.saxonica.com/

 

-----Original Message-----
From: DEVAL SHAH [mailto:devals9(_at_)hotmail(_dot_)com] 
Sent: 04 March 2006 00:05
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Passing a Map as parameter to XSLT.

Hello,
Thank you for the reply.

I am still not able to access the parameter that I pass to XSLT.

Let me explain what I want to do.

I want to pass in key,value pairs to XSLT. I hava a Map 
object in Java which 
contains this key value pair. I read in some forum that there 
is no direct 
way to pass this object as a Map so I need to convert it into 
xml format. So 
I am converting it into a xml format and setting it as the 
parameter. I am 
using javax.xml.transform.Transformer. I set the parameter as:
transformer.setParameter("XSLTParameter",xmlDoc);
where xmlDoc is some org.w3c.dom.Document object.

Now in xsl I try to get this parameter using <xsl:param 
name="XSLTParameter" 
/>.
I want to loop through the xml document that I have passed in 
XSLTParameter 
using some key.
So I define
<xsl:key name="xsltParameterMap" match="/*/map/entry" use="@key"/>

I Tried to loop in following manner.

<xsl:for-each select="key('xsltParameterMap','line')">
.........
</xsl:for-each>

XSLT is not going inside this loop. I am sure I am setting 
'line' as one of 
the key value in the XML that I am passing as parameter to 
the XSLT. So it 
should go inside the loop.

Any help on this ?

Thanks
Deval

From: "Michael Kay" <mike(_at_)saxonica(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: RE: [xsl] Passing a Map as parameter to XSLT.
Date: Fri, 3 Mar 2006 22:57:18 -0000


I want to pass a java.util.Map object to a XSLT as a parameter.

Why do you want to pass a java.util.Map, and if that's what 
you want to do,
why are you passing an org.w3c.dom.Document?

Currently the way I am doing is:
1. Creating an org.w3c.dom.Document object in the format
    <x:map>
    <entry key="..." value="..."/>
          ...
    </x:map>

2. I have defined a parameter in xslt
<xsl:param name="XSLTParameter" />


Now how do I access this map in the XSLT.

Your application has to tell the XSLT processor to use the 
document(1) as
the value of the parameter(2). The way you do this depends on your
processor. Look for a method called setParameter() or addParameter().

Then you access the parameter as $XSLTParameter.

 I am not able to access it.
I was trying to use the key function as follows:

<xsl:key name="xsltParameterMap" match="document('')/*/map/entry"
use="@key"/>


A key might be useful to find an entry within your map, but 
it isn't going
to help you access the map. Once you've loaded the document 
you can define 
a
key as

 <xsl:key name="xsltParameterMap" match="/*/map/entry" use="@key"/>

(though match="entry" will work equally well)

and then access a value with

key('xsltParameterMap', $requiredValue)

having made sure that $XSLTParameter is the context node.

Michael Kay
http://www.saxonica.com/



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--