www.webdeveloper.com
Results 1 to 3 of 3

Thread: Problem with JavaScript/XSL

  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Problem with JavaScript/XSL

    Hi Folks,

    Long time reader - First time poster.

    I haven't long started programming with Ajax technology and I have cropped upon an issue that has really stumped me.

    I have an XSL file with the following contents:

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    	<body>
       		<h2>Footballl</h2>
        	<table border="1">
    	      <tr bgcolor="#9acd32">
    		      <th>Player</th>
    		      <th>Team</th>
    		      <th>Number</th>
    		      <th>Position</th>
    		      <th>Field Area</th>
    		 </tr>
    	<xsl:for-each select="player-list/player">
        	<xsl:if test="position='Defender' and fieldarea='Right Back'">
            	    <tr>
    			        <td><xsl:value-of select="name"/></td>
    			        <td><xsl:value-of select="team"/></td>
    			        <td><xsl:value-of select="number"/></td>
    			        <td><xsl:value-of select="position"/></td>
    			        <td><xsl:value-of select="fieldarea"/></td>
    				</tr>
        	</xsl:if>
        </xsl:for-each>
        </table>
      </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    The XSL displays the initial output onto the web page: www.dbirkin.co.uk/uni/wdd2

    However, when using JavaScript pass changes into the XSL:IF test statement to change the data on the screen.

    The JavaScript code is as follows:

    Code:
    var xml, xsl; 
    xml = loadXMLDoc("xml/players/players.xml");
    xsl = loadXMLDoc("xml/players/players.xsl");
    
    function loadXMLDoc(dname)
    {
    if (window.XMLHttpRequest)
      {
      xhttp=new XMLHttpRequest();
      }
    else
      {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open("GET",dname,false);
    xhttp.send("");
    return xhttp.responseXML;
    }
    
    function loadPlayerData(){
    	//xml=loadXMLDoc("xml/players/players.xml");
    	//xsl=loadXMLDoc("xml/players/players.xsl");
    	displayPlayerResult();
    }
    
    
    function displayPlayerResult()
    {
    // code for IE
    if (window.ActiveXObject)
      {
      ex=xml.transformNode(xsl);
      document.getElementById("players").innerHTML=ex;
      }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
      {
      xsltProcessor=new XSLTProcessor();
      xsltProcessor.importStylesheet(xsl);
      resultDocument = xsltProcessor.transformToFragment(xml,document);
      document.getElementById("players").innerHTML="";
      document.getElementById("players").appendChild(resultDocument);
      }
    }
    
    function MM_jumpMenu1(selObj, selObj2)
    { 
    var selObj = document.getElementById("positionmenu");  // Set value of Drop Down Menu 1's Selected Value
    var selObj2 = document.getElementById("field"); // Set value of Drop Down Menu 2's Selected Value
    
    
    	x=xsl.getElementsByTagName("xsl:if")[0]; // sets element for ie
    	alert(x); // Returns a "Unspecified" or "null" Error
    	if (x==null) { 
    		x=xsl.getElementsByTagName("if")[0];// sets element for other browsers
    		alert(x); // Returns an "unspecified" or "null" error
    	}
    	var testoption = ("position='" + selObj.value + "' and fieldarea='" + selObj2.value + "'"); // Concatanated string for input to the TEST statement
    	alert (testoption);
    	x.setAttribute("test", testoption); //"position='Defender'"
    	displayPlayerResult();  
    }
    The code in RED is where my error is occuring. Object is apparently unspecified and the browser is not recognising the XSL:IF attribute within the XSL file. I don't know if you need to state XSL:IF differently but when i used the same function with XSL:SORT it works fine.

    Any help would be highly appreciated.

    Many thanks,

    David

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Your 'fixtures.js' file and 'players.js' file each use the same global variables, 'xml' and 'xsl', change to using different globals for the fixtures.js file.

  3. #3
    Join Date
    Nov 2009
    Posts
    2
    Hi, astupidname....

    Your solution worked absolutely perfect!

    I highly appreciate your response.

    Just for other people: Do not declare the same Global Variable within 2 or more documents.

    Thank you!

    David

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles