/    Sign up×
Community /Pin to ProfileBookmark

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>
[/CODE]

The XSL displays the initial output onto the web page: [url]www.dbirkin.co.uk/uni/wdd2[/url]

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

[COLOR=”Red”]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
}[/COLOR]
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();
}
[/CODE]

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

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameNov 20.2009 — 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.
Copy linkTweet thisAlerts:
@davidbirkin1988authorNov 21.2009 — 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
×

Success!

Help @davidbirkin1988 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...