Jump to content

Javascript ifs in xsl


trainzebra

Recommended Posts

Hey all. I'm running into a small problem while trying to use javascript if statements in an xsl file. I've tried a couple different solutions. I'm just going to go with the simplest example I have here with simply an image and no xsl. I want to display the image blue1.jpg when the variable str = "started".When I attempt it this way, the image is displayed no matter what str is:

<script type="text/javascript">if(str == "started"){</script>	<td><img src="blue1.jpg"></img></td><script type="text/javascript">}</script>

When I attempt it this way, there's a conflict between what javascript and xsl want. If I put a backslash before the quotes in blue1.jpg to suppress their meaning, xsl throws an error because it expects a quote after src=. If I don't put them there, javascript reads the first one as the end of the string and doesn't display anything.

<script type="text/javascript">if(str == "started"){	  document.write("<td><img src="blue1.jpg"></img></td>")}</script>

Does anyone know of any way to work around this?

Link to comment
Share on other sites

XSLT is a transformation language. Think of what your end code is supposed to look like and think how you want to manipulate the ouput, not how you want to manipulate the XSLT and/or XML itself.Or better yet, do think how you want to manipulate the XML and XSLT. But do not expect one to be aware of the other.Which brings us to your problem. If what I think is right

<script type="text/javascript">if(str == "started"){	  document.write("<td><img src="blue1.jpg"></img></td>")}</script>

Is actually referring to a rewrite in the XSLT itself. So, what you've define is that when str == "started", your complete XSLT will look like

<td><img src="blue1.jpg"></img></td>

which is obviously not a valid XSLT stylesheet. Instead, access the current element and use write() over it.

Link to comment
Share on other sites

Thanks for the help Boen. Forgive me if I'm being slow here, but what do you mean by access the current element and write it? Let me expand my code a little to see if there's anything you can point out to help me understand. Str is a variable that contains the value of a query string appended to my xml file's url on a button press, for reference. At various points in my code, I want to display one of three colored icons based on str's value (I'll probably use a switch statement when finalizing my code).

<xsl:for-each select="varname1">	 <script type="text/javascript">			if(str == "started"){				  document.write("<td><img src="blue1.jpg"></img></td>");				  document.write("<td><xsl:value-of select="../@varname2"/></td>");			} 			else if(str == "completed"){				  document.write("<td><img src="green1.jpg"></img></td>");				  document.write("<td><xsl:value-of select="../@varname2"/></td>");			} ...	  </script></xsl:for-each>

If my understanding is correct then my xsl for this particular code segment would look like this?

<xsl:for-each select="varname1">	 <td><img src="blue1.jpg"></img></td>	 <td><xsl:value-of select="../@varname2/></td></xsl:for-each>

Is this also an invalid stylesheet? When simply coded it displays just fine, I just can't seem to get javascript to output this text using write(). Thanks again for the help.

Link to comment
Share on other sites

What you're showing is still part of the complete XSLT file. That's why I bolded the word "complete". Imagine your whole XSLT file looking like any of the above fragments without anything else in it. It won't be valid.By "access" an element I mean... well... how should I put it. With

document.write("something")

you write "something" over the whole document. The complete source code of the currect document becomes "something". And with

document.getElementById("var").write("something")

you write "something" inside an element inside the document, that has an ID of "var". So, by using getElementById(), you've accessed an element with the targeted ID. By using XML DOM objects, you can access nodes in an XML based document and add/change/remove such.Your XSLT approach seems a lot better. Infact, here's an idea. Perform a check in XSLT based on the value of an XML node (be it attribute or element). Then, with a JavaScript inside the XSLT, change the value of that node or better yet - just create a new such node with the specified value. The XSLT should then redo the transformation, taking the new value into account.

Link to comment
Share on other sites

That's an extremely good idea. Can you clarify if I'm going about this the right way? Relevant parts of the XML file are as follows.

<?xml version="1.0" encoding="utf-8"?><PLMXML <!--lots of attributes--> ><Header id="id1"></Header></PLMXML>

I'm attempting to get the Header node and write into it.

document.getElementById("id1").write("something")<!-- or -->document.getElementById("Header").write("something")

However when I try to output it, nothing comes back.

<xsl:value-of select="PLMXML/Header"/>

Is my procedure right here? If I need to I can probably get the name of the xml file from window.location and make an ActiveXObject to access the full tree, but if there's a simpler way I'd prefer it =p

Link to comment
Share on other sites

The first

document.getElementById("id1").write("something")

is closer. However, the major problem is that you're accessing the current document. You need to first load the XML document (from the window.location as you're suggesting) and then find the node and set it. Something like that maybe (not tested and will probably not work):

var xmlLocation = window.location;xmlDoc=loadXMLDoc(xmlLocation);var x=xmlDoc.getElementsByTagName("Header"); for(i=0;i<x.length;i++)  {  x.item(i).setAttribute("GET-KEY","GET-VALUE");  }

And in XSLT then, the check would be:

<xsl:if test="@GET-KEY = 'GET-VALUE'"><!--Whatever actions--></xsl:if>

I'm not good in XML DOM, so I can't really help you for editing the contents of the Header.What you were thinking is probably the only method.

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...