Reputation: 117
Is it possible to create a xsl variable using
<xsl:variable name="x"><xsl:value-of select="country" /></xsl:variable>
using a javascript variable?
The variable "country" is created in another javascript file, that is not embedded within the xslt. I am trying to get the country of user's location using ajax.
--------------Javascript File--------------
$(document).ready(function()
{
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$.getJSON('http://ws.geonames.org/countryCode', {
lat: position.coords.latitude,
lng: position.coords.longitude,
type: 'JSON'
}, function(result) {
country = result.countryCode;
});
});
};
If there is a better way, please do reply. Thanks in advance.
Upvotes: 0
Views: 2962
Reputation: 163352
The most common way of using client-side XSLT is for the XSLT to generate the HTML page, including its Javascript. With this approach, the XSLT stops executing before the JS starts executing, so there's no way the JS can influence the XSLT. But of course it is also possible to run an XSLT transformation programmatically, invoking it via a Javascript API, in which case you can pass information into the transformation by setting parameters using that API.
Upvotes: 0
Reputation: 101700
If you want to pass values into an XSL, the typical mechanism is to use a parameter. At the top level of your XSL, you would declare the parameter:
<xsl:stylesheet ....>
.....
<xsl:param name="country" />
.....
And then you would pass that value in from your external code, indicating that the value is intended for the "country" parameter.
I don't have experience using XSLT parameters in JavaScript, but this tutorial seems to have information on how to do so:
Passing XSLT parameters with JavaScript
Mozilla also provides this information, though I'm not sure how cross-browser compliant either approach is:
Using the Mozilla JavaScript interface to XSL
Upvotes: 1