...

View Full Version : Javascript and XML round 2



utahman3431
06-10-2008, 10:49 PM
I am using javascript to display a menu that is pulled from an xml document and sorted alphabetically using xslt. I want to be able to click on a link and display the data on a different <div> on the webpage. The script I have to display the information is:


function show(i)
{

title=(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
body=(x[i].getElementsByTagName("body")[0].childNodes[0].nodeValue);

txt= "<center><h2>" + title + "</h2></center><br />" + body;

document.getElementById("mainbdy").innerHTML=txt;
document.getElementById("subnav").style.visibility='hidden';
return false;
}


the only problem is when I click on a link from my sorted list that isn't originally in the XML document alphabetically, it displays the information in that location of the XML page. I was hoping I could just use a different item other than "i" to choose which part of the XML document to display. Each item has a unique <name> tag. Is it possible to get the information using some variable associated to <name> rather than "i"?

utahman3431
06-12-2008, 01:29 AM
Ok. I haven't been able to find any new information for the above problem so I'll try to attack it from a different angle.

I have the following code that will import an xsl file to sort and display some data in an xml document:



function loadXMLDoc(fname)
{
var NewxmlDoc;
// code for IE
if (window.ActiveXObject)
{
NewxmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation
&& document.implementation.createDocument)
{
NewxmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
NewxmlDoc.async=false;
NewxmlDoc.load(fname);
return(NewxmlDoc);
}

function displayResult()
{
xml=loadXMLDoc("support_docs.xml");
xsl=loadXMLDoc("support_docs.xsl");
// code for IE
if (window.ActiveXObject)
{
ex=xml.transformNode(xsl);
document.getElementById("articlestuff").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("articlestuff").appendChild(resultDocument);
}

}


Here is the code for the xsl stylesheet:



<?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="/">
<ul id="subnav">
<xsl:for-each select="help_articles/support_doc">

<xsl:sort select="header" />
<li>
<a href="javascript://" onclick="show({position()-1})">
<xsl:value-of select="header" />
</a>
</li>
</xsl:for-each>
</ul>
</xsl:template>

</xsl:stylesheet>


Is it possible to add a template to the end of this stylesheet and create a new script that will call just that template, or am I just going to have to create a completely separate stylesheet?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum