Jump to content

xsl in Jscript, and passing Jscript vars in xsl


wallyson

Recommended Posts

Here is snippet of my xml :<Holidays>	<Months>		<Data>			<Name>August</Name>		</Data>	</Months>	<Days>		<Data>			<Day>1</Day>			<Holiday>Emancipation Day</Holiday>			<Country>(BRB, JAM, TTO)</Country>		</Data>		<Data>			<Day>1</Day>			<Holiday>Parents Day</Holiday>			<Country>(COG)</Country>		</Data>............................................Ok here is a Jscript function in my XSL code:function showCountries(pass){	var results ='';	var sendtoDIVid = "requests"; 	switch(pass)	{		case pass:			results +='<p style="color: Navy">Countrys</p>'+			'<p style="color: Navy">'+pass+' 2006</p>'+			'<ul>'+']]><xsl:for-each select="Holidays/Days/Data[Holiday = '+pass+']"><![CDATA['+			  '<li style="color: navy">'+']]><xsl:value-of select="Country"/><[CDATA[</li>'+					']]></xsl:for-each><![CDATA['+			'</ul>'+			'</table>';						document.getElementById(sendtoDIVid).innerHTML = results;		break;		default: 	}}]]></script>...............................................Ok the pass variable comes in and has a holiday for example 'Emancipation Day', that variable is then Checked agaisnt Holiday, then it will print out the country that celebrates 'Emancipation Day'.Well it doesnt work, I know my logic is right becuase if type in 'Emancipation Day'where [Holiday = 'pass'] is at (so: [Holiday = 'Emancipation Day']) it works.How do I get that Jscript variable to work?PS. I already tried writing it so that pass had single quotes " ' " around it.Please help.

Link to comment
Share on other sites

Well, you certainly don't have a "Holiday" element, which value is eaxctly '+pass+' and that's your problem.What exactly are you trying to achieve?

Link to comment
Share on other sites

Well, you certainly don't have a "Holiday" element, which value is eaxctly '+pass+' and that's your problem.What exactly are you trying to achieve?
Holiday is in the XML file.pass is sent from another function called showHolidays.pass will be a holiday.then in the xsl the the Holiday element will check pass and if they equal, it will print out that country.It works when you type in the name of a country, but i dont wnat it to that obviously. Some wierd reason it wont check agiasnt that Jscript Variable. This goal is easy, i have an xml file with countries and holidays, im making it sort through them.what you dont see is the drop down were the user selects a month.then all the holidays in that month are shown.then you click on a holidaythen the countries show up that celebrate that holiday.its messing up at the showing the countries part. and thats why i posted.
Link to comment
Share on other sites

Well, the reason for your problem is in your description of it:

Some wierd reason it wont check agiasnt that Jscript Variable.
XSLT expects an XSLT variable/parameter or a direct node set (XPath expression). It can't process variables from another language.What you can do is to store the pass as a XSLT parameter, and use that parameter's value instead.
<xsl:param name="pass"/>function showCountries(pass){	var results ='';	var sendtoDIVid = "requests"; 	switch(pass)	{		case pass:			results +='<p style="color: Navy">Countrys</p>'+			'<p style="color: Navy">'+pass+' 2006</p>'+			'<ul>'+']]><xsl:for-each select="Holidays/Days/Data[Holiday = $pass]"><![CDATA['+			  '<li style="color: navy">'+']]><xsl:value-of select="Country"/><[CDATA[</li>'+					']]></xsl:for-each><![CDATA['+			'</ul>'+			'</table>';						document.getElementById(sendtoDIVid).innerHTML = results;		break;		default: 	}}]]></script>

then, in the output, you need to make a call to another function, which will change that parameter's value. When that value is changed, so is the value of this script. this post demostrates a JavaScript function that is going to change the parameter. I'm not good at JavaScript, so I can't adjust that script for you.

Link to comment
Share on other sites

Well, the reason for your problem is in your description of it:XSLT expects an XSLT variable/parameter or a direct node set (XPath expression). It can't process variables from another language.What you can do is to store the pass as a XSLT parameter, and use that parameter's value instead.
<xsl:param name="pass"/>function showCountries(pass){	var results ='';	var sendtoDIVid = "requests"; 	switch(pass)	{		case pass:			results +='<p style="color: Navy">Countrys</p>'+			'<p style="color: Navy">'+pass+' 2006</p>'+			'<ul>'+']]><xsl:for-each select="Holidays/Days/Data[Holiday = $pass]"><![CDATA['+			  '<li style="color: navy">'+']]><xsl:value-of select="Country"/><[CDATA[</li>'+					']]></xsl:for-each><![CDATA['+			'</ul>'+			'</table>';						document.getElementById(sendtoDIVid).innerHTML = results;		break;		default: 	}}]]></script>

then, in the output, you need to make a call to another function, which will change that parameter's value. When that value is changed, so is the value of this script. this post demostrates a JavaScript function that is going to change the parameter. I'm not good at JavaScript, so I can't adjust that script for you.

Thanks man, but I get a "cant use key word param here" when trying to make the convert, do you know any other tricks with working with other languages variables? thanks Andrew
Link to comment
Share on other sites

Try to place the <xsl:param> element as a top level element. In other words, outside a template. Something like:

<xsl:stylesheet version="1.0" ...><xsl:param name="pass"/><xsl:template match="/">...</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Try to place the <xsl:param> element as a top level element. In other words, outside a template. Something like:
<xsl:stylesheet version="1.0" ...><xsl:param name="pass"/><xsl:template match="/">...</xsl:template></xsl:stylesheet>

I did that and it seems to work, but when I alert the pass value to check it, its empty. The pass is empty.
Link to comment
Share on other sites

I did that and it seems to work, but when I alert the pass value to check it, its empty. The pass is empty.
Well, the script I showed you activates when called (either with a click on link with approariate href, or in a script).You could add the select attribute with a default value though:
<xsl:param name="pass" select="'some value you want to use as a default one'"/>

If you've adjusted the script properly, the parameter should be changed on the appropriate event. Note that the selected value must match exactly the content of the element you're aiming at. For example:

<xsl:param name="pass" select="'Emancipation Day'"/>

Link to comment
Share on other sites

Well, the script I showed you activates when called (either with a click on link with approariate href, or in a script).You could add the select attribute with a default value though:
<xsl:param name="pass" select="'some value you want to use as a default one'"/>

If you've adjusted the script properly, the parameter should be changed on the appropriate event. Note that the selected value must match exactly the content of the element you're aiming at. For example:

<xsl:param name="pass" select="'Emancipation Day'"/>

Check this out.
function showCountries(pass){	var results ='';	var sendtoDIVid = "requests"; ']]><xsl:variable name="fool">'+pass+'</xsl:variable><![CDATA[';// convert pass to xsl variable	switch(pass)	{		case pass:			results +='<p style="color: Navy">Countrys</p>'+			'<p style="color: Navy">'+pass+' 2006</p>'+			'<ul>'+	']]><xsl:for-each select="Holidays/Days/Data[Holiday = $fool]"><![CDATA['+ //Holiday checks agaisnt xsl variable $fool.	'<li style="color: navy">'+']]><xsl:value-of select="Country"/><![CDATA[</li>'+//the countries of that holiday print out.	']]></xsl:for-each><![CDATA['+			'</ul>'+			'</table>';					document.getElementById(sendtoDIVid).innerHTML = results;		break;		default: 	}}

I converted the jscript pass variable into a xsl variable, then I use that. BUT it doesnt work, but I know my logic is correct, cas when you take this: ]]><xsl:variable name="fool">'+pass+'</xsl:variable><![CDATA[';// convert pass to xsl variableand change it like this:]]><xsl:variable name="fool">Emancipation Day</xsl:variable><![CDATA[';// convert pass to xsl variableThe stupid thing works!!!!!!????!!! Oh man Im about to start over and simply use a jscript parser I already have. This if my first time with xsl and I really dig it alot cas its so simple. I dont want to start over.

Link to comment
Share on other sites

What I'm trying to say from the start is that you can't convert a JavaScript variable to XSLT one. Not only that, but you can't use JavaScript values with XSLT in any way. You can however convert XSLT variable/parameter to a JavaScript one, and/or you can use JavaScript to adjust a parameter's value, thus influence on the output. You must rethink your approach to the thing you're trying to achieve... whatever it is (I still can't grasp it. I need markup).

Link to comment
Share on other sites

What I'm trying to say from the start is that you can't convert a JavaScript variable to XSLT one. Not only that, but you can't use JavaScript values with XSLT in any way. You can however convert XSLT variable/parameter to a JavaScript one, and/or you can use JavaScript to adjust a parameter's value, thus influence on the output. You must rethink your approach to the thing you're trying to achieve... whatever it is (I still can't grasp it. I need markup).
Hey man I really appreciate all your help, I simply started over and using a jscript parser I have already made. xsl is really nice, but man do they need to modify it so that it can understand other languages. Its ok when your not using javascript so much with hard functions. I even had a jscript and xsl guy of like 10 years help me out, and we couldnt figure it out. Sad to say, but I probably wont be using xsl again, simply becuase its a headache when the code gets complicated. Still kool non the less.
Link to comment
Share on other sites

The reason XSLT won't be make to udnerstand JS is that it's a transformation language. It's the bridge between XML and XHTML, not the bridge from one XHTML to another slightly different XHTML like JS is.Think of it like PHP. The output is what is processed, but PHP can't directly grab a JS's value (unless we're talking AJAX, but that's another thing).It's your choise if you'll use it or not though. I'm just saying it's your approach that needs rethinking, not the language being reinvented.

Link to comment
Share on other sites

The reason XSLT won't be make to udnerstand JS is that it's a transformation language. It's the bridge between XML and XHTML, not the bridge from one XHTML to another slightly different XHTML like JS is.Think of it like PHP. The output is what is processed, but PHP can't directly grab a JS's value (unless we're talking AJAX, but that's another thing).It's your choise if you'll use it or not though. I'm just saying it's your approach that needs rethinking, not the language being reinvented.
thanks, well I am still using xpath to do my deeds. Now im having troulbe creating a no dupelicates query. becuase thats basically what this is, I dont want any duplicates to print out.im using javascript to make the query. Do you know of any good get rid of duplicate sorts?
Link to comment
Share on other sites

You mean you want to perform an XPath expression that returns only distinct values?There are three ways:1. The XPath 2.0 function disting-values(). Not supported by most of today's XSLT processors, so it's probably out of the question.2. The set:distinct() EXSLT function. It's not supported across XSLT processors though. If you're using XSLT on the client side, then it's not supported for sure.3. Download the set:distinct EXSLT template and include it in your stylesheet. Call it like this:

<xsl:call-template name="set:distinct">   <xsl:with-param name="nodes" select="node-set" /></xsl:call-template>

Link to comment
Share on other sites

You mean you want to perform an XPath expression that returns only distinct values?There are three ways:1. The XPath 2.0 function disting-values(). Not supported by most of today's XSLT processors, so it's probably out of the question.2. The set:distinct() EXSLT function. It's not supported across XSLT processors though. If you're using XSLT on the client side, then it's not supported for sure.3. Download the set:distinct EXSLT template and include it in your stylesheet. Call it like this:
<xsl:call-template name="set:distinct">   <xsl:with-param name="nodes" select="node-set" /></xsl:call-template>

Im not sure if I can use a template with what im doing. inside the html page, I use javascript to load a xml file, then i do: nodes = xmldoc.selectNodes("xpath query goes here");nodes is then going to have the xml data that is sorted. its basicaly xsl in javascript (sorta) becuase its using xpath to make the quieries. so Im thinking loading a template is out of the question. which leads me to this question, is there a duplicate query that will work with this.
Link to comment
Share on other sites

Oh, pure JavaScript way. Um... no. You'll have to wait until the distinct-values() function is supported.The only workaround I can think of is if you query every element of the targeted group and compare it's value with the previous one, then output it if it's not duplicated. I'm not good at JS, so I can't tell you how exactly to do this.

Link to comment
Share on other sites

Oh, pure JavaScript way. Um... no. You'll have to wait until the distinct-values() function is supported.The only workaround I can think of is if you query every element of the targeted group and compare it's value with the previous one, then output it if it's not duplicated. I'm not good at JS, so I can't tell you how exactly to do this.
th js way still incorporates the xpath way but its only a string like:nodes = xmldoc.selectNodes("Holiday/Data[Month = 'August']");I have tried that and it works, printing out all the holidays in August. but then again getting rid of duplicates im not good with the xpath syntax so I wouldnt even no where to begin with an xpath expression to check and stuff.
Link to comment
Share on other sites

I'm telling you, distinct-values() is the real deal, but it's simply not supported yet:

nodes = xmldoc.selectNodes("distinct-values(Holiday/Data[Month = 'August'])");

There isn't any other XPath only way to do this. Scripting can only do it's way around it with more scripting.

Link to comment
Share on other sites

I'm telling you, distinct-values() is the real deal, but it's simply not supported yet:
nodes = xmldoc.selectNodes("distinct-values(Holiday/Data[Month = 'August'])");

There isn't any other XPath only way to do this. Scripting can only do it's way around it with more scripting.

Hey man, Thanks for the help! I actually got it all to work now. I used javascript algo to fix my duplicate problem, and I also used the bubble sort to sort it by day (1, 2, 3 ....) You were very helpful.Again thanks much,Wally
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...