0

I would like to know if it's possible to include an "external" variable (javascript in my case) in a xsl condition.

In my example, I get a coordinate on a map with some javascript, my xMax var. And I would like to use this xMax var in a "xsl:if" to find all the 'entries' for which the latitude is inferior or equal to xMax.

An excerpt of my script :

var cpt = 0;
var xMax = map.getBounds().getNorthEast().lat();

<xsl:for-each select="entries/entry">
   <xsl:if test="latitude &lt;= xMax">
        cpt++;
    </xsl:if>
</xsl:for-each>
alert(cpt);

I found the "msxsl:script" element, and i tried this :

<msxsl:script language="JScript" implements-prefix="user">
    function getXMax(){
        var xMax = map.getBounds().getNorthEast().lat();
        return xMax;
    }
</msxsl:script>

and called it with :

<xsl:value-of select='user:getXMax(.)'/>

But it doesnt seems to work. (I have also included

xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://localhost:8080/Projet/map"

in the xsl:stylesheet)

If someone know how to pass my js var into the condition or how to parse a js var to a xslt var, thank for any help !

1
  • 3
    Conceptually, the easiest solution would be to pass in a stylesheet parameter. The details of doing so will depend on which XSLT processor and API you're using. I don't do Javascript so I don't have a more specific answer, but knowing what you're looking for may help you get there. Commented Dec 29, 2013 at 21:37

2 Answers 2

4

Mixing JavaScript and XSLT in the way you are hoping is not really possible.

If XSLT is being used to convert HTML and JavaScript from XML, then the JavaScript that is being generated is not actually being executed at that point. As far as XSLT is concerned it is just outputting a string of characters which will later be parsed (by the browser) as JavaScript. By the time the browser kicks in to process the JavaScript then the XSLT will be long forgotten. It will be treated no differently to if you typed in the JavaScript in Notepad yourself.

As you have shown, you can have JavaScript functions in the XSLT, but this is totally separate to the JavaScript you are outputting, and can only be used by the XSLT in the course of its execution.

So, you will need a slightly different approach. You can perform XSLT transformations via JavaScript, and pass parameters for this XSLT transformation. However, this might not necessarily be the ideal approach here.

One approach you could take is to output the relevant XML (the entry elements) as JSON, which you can then access in JavaScript. So, the logic of which latitude values is less than the xMax variable is checked entirely within JavaScript.

So, for example, your XSLT may look like this...

<script>
var cpt = 0;
var xMax = map.getBounds().getNorthEast().lat();

var coordinates = [
<xsl:for-each select="entries/entry">
   <xsl:if test="position() > 1">,</xsl:if>
   {
      "lat":<xsl:value-of select="latitude" />,
      "long":<xsl:value-of select="longitude" />
   }
</xsl:for-each>
];
for (var i = 0; i &lt; coordinates.length; i++)
{
   if (coordinates[i].lat &lt; xMax)
      cpt++;
}
alert(cpt);
</script>

Then, given this XML

<entries>
<entry>
<latitude>1</latitude>
<longitude>2</longitude>
</entry>
<entry>
<latitude>3</latitude>
<longitude>4</longitude>
</entry>
</entries>

This would generate JavaScript that looked something like this

<script>
var cpt = 0;
var xMax = map.getBounds().getNorthEast().lat();

var coordinates = [
   {
      "lat":1,
      "long":2
   }
   ,
   {
      "lat":3,
      "long":4
   }
];
for (var i = 0; i < coordinates.length; i++)
{
   if (coordinates[i].lat < xMax)
      cpt++;
}
alert(cpt);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Thank you for your help and the explications about how javascript works with xslt.

I used your approach with JSON and it worked perfectly.

For a moment I didn't understand why it didn't work, but i just had to add a condition when latitude and longitude aren't nulls, as bellow :

var coordinates = [
    <xsl:for-each select="entries/entry">
        <xsl:if test="latitude != '' and longitude != ''">
            <xsl:if test="position() > 1">,</xsl:if>
            {
                "lat":<xsl:value-of select="latitude" />
                "long":<xsl:value-of select="longitude" />
            }
    </xsl:for-each>
];

Thank you again (and happy new year).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.