Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advice on XML

Status
Not open for further replies.
Apr 20, 2005
27
GB
Hi

I have a solution to an XML problem which is written in JS and is client side. This causes compatibility problems and doens't work in FF, unfortunately.

I'm looking into making the application server-side - which, I think, will solve my problems.

I'm wondering if anyone could give me any advice here? Any help much appreciated.
 
Java, ASP and PHP all have good built-in methods for dealing with XML. What you trying to do?

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Jonty - I think my original solution could still be used. My original JavaScript failed repeatedly in FF but it seems this could be sorted. Please could you take a look at the above post and let me know what you think?
 
There was a particular line in some code I think you originally posted.

xmldone=xml.load("results.xml");

Fails in FF because it doesn't give it a chance to load. Is there any way of sorting this in the browser?
 
Yeah, that code was rubbish. Try this:
Code:
var xml,xsl;
function go() 
{
  if(document.implementation && document.implementation.createDocument)
  { 
    // Mozilla 

    // Load XML
    xml = document.implementation.createDocument("", "", null);
    xml.addEventListener('load', loadXSL, false);
    xml.load("results.xml");
  }
  else if(window.ActiveXObject)
  { 
    // IE

    // Load XML
    xml = new ActiveXObject("MSXML2.DOMDocument");
    xml.async = false
    xml.load("results.xml") 
    
    // Load XSL
    xsl = new ActiveXObject("MSXML2.DOMDocument");
    xsl.async = false
    xsl.load("stylesheet.xsl")

    //Change parameter
    xsl.selectSingleNode("//xsl:param[@name='fieldvalue']").setAttribute("select", "'"+document.forms[0].fieldvalue.value+"'")
    
    // Transform
    document.write(xml.transformNode(xsl))
  }
  else
  {
    // Browser unknown
    alert("Browser unknown");
  }
}
function loadXSL()
{
  // Load XSL
  xsl = document.implementation.createDocument("", "", null);
  xsl.addEventListener('load', doTransform, false);
  xsl.load("stylesheet.xsl");
}
function doTransform()
{
  xsltProcessor = new XSLTProcessor();
  xsltProcessor.importStylesheet(xsl);

  //Change parameter
  xsltProcessor.setParameter(null, "fieldvalue", document.getElementsByName("fieldvalue")[0].value);
    
  // Transform
  result = xsltProcessor.transformToDocument(xml);
  document.removeChild(document.firstChild);
  document.appendChild(result.firstChild);
}

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Jonty, thanks for that. Again, it works fine in IE, but in FF, brings the message:

Error: xsl is not defined
Source File: formscript.js
Line: 49

I also get:

Error: doTransform is not defined
Source File: formscript.js
Line: 43

Or sometimes:

Error: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)" location: " Line: 57"]
Source File: formscript.js
Line: 57

These messages appear 'randomly', in that when I submit the form, a different message appears each time.

My source file is:

var xml,xsl;
function uponSubmit()
{
if(document.implementation && document.implementation.createDocument)
{
// Mozilla

// Load XML
xml = document.implementation.createDocument("", "", null);
xml.addEventListener('load', loadXSL, false);
xml.load("results.xml");
}
else if(window.ActiveXObject)
{
// IE

// Load XML
xml = new ActiveXObject("MSXML2.DOMDocument");
xml.async = false
xml.load("page.xml")

// Load XSL
xsl = new ActiveXObject("MSXML2.DOMDocument");
xsl.async = false
xsl.load("stylesheet.xsl")

//Change parameter
xsl.selectSingleNode("//xsl:param[@name='url']").setAttribute("select", "'"+document.forms[0].url.value+"'")

// Transform
document.write(xml.transformNode(xsl))
}
else
{
// Browser unknown
alert("Browser unknown");
}
}
function loadXSL()
{
// Load XSL
xsl = document.implementation.createDocument("", "", null);
xsl.addEventListener('load', doTransform, false);
xsl.load("stylesheet.xsl");
}
function doTransform()
{
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);

//Change parameter
xsltProcessor.setParameter(null, "url", document.getElementsByName("url")[0].value);

// Transform
result = xsltProcessor.transformToDocument(xml);
document.removeChild(document.firstChild);
document.appendChild(result.firstChild);
}

I've changed a few variable names. My form value ('fieldvalue') is now named 'url'. The source page is now page.xml (rather than 'results.xml') the method which is run upon the user clicking Submit is uponSubmit().
 
You have changed the xml file in the mozilla bit:
Code:
...xml.load("results.xml");.....

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Sorry, I've changed that bit to pages.xml. However, I get the following problems:

Error: xsl is not defined
Source File: formscript.js
Line: 49

Error: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXSLTProcessor.transformToDocument]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: formscript.js :: doTransform :: line 55" data: no]
Source File: formscript.js
Line: 55

Error: doTransform is not defined
Source File: formscript.js
Line: 43
 
Please could we move to the JS form so I don't get kicked off for using the wrong area?
 
Dont see why this forum's not ok. Its an XML question. I don't know whats wrong with your code. It works for me. Are you sure you are declaring the variables (var xml, xsl;)? Post your full html page, your js, your xsl and xml(or links to them) and I might be able to help more.

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
I am stumped. This is giving me a headache. I don't know why the xsl variable isnt recognised. When you take it out of the if statement, it works, but you then get a security problem with getting a page from another domain. I'd go for a server-side solution if I were you.

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Hmmm, this is strange. I'm not declaring the XSL variable specifically, I'm just copying what you've written into my JS code:

var xml,xsl;
function uponSubmit()
{
if(document.implementation && document.implementation.createDocument)
{
// Mozilla

// Load XML
xml = document.implementation.createDocument("", "", null);
xml.addEventListener('load', loadXSL, false);
xml.load("page.xml");
}
else if(window.ActiveXObject)
{
// IE

// Load XML
xml = new ActiveXObject("MSXML2.DOMDocument");
xml.async = false
xml.load("page.xml")

// Load XSL
xsl = new ActiveXObject("MSXML2.DOMDocument");
xsl.async = false
xsl.load("stylesheet.xsl")

//Change parameter
xsl.selectSingleNode("//xsl:param[@name='url']").setAttribute("select", "'"+document.forms[0].url.value+"'")

// Transform
document.write(xml.transformNode(xsl))
}
else
{
// Browser unknown
alert("Browser unknown");
}
}
function loadXSL()
{
// Load XSL
xsl = document.implementation.createDocument("", "", null);
xsl.addEventListener('load', doTransform, false);
xsl.load("stylesheet.xsl");
}
function doTransform()
{
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);

//Change parameter
xsltProcessor.setParameter(null, "url", document.getElementsByName("url")[0].value);

// Transform
result = xsltProcessor.transformToDocument(xml);
document.removeChild(document.firstChild);
document.appendChild(result.firstChild);
}

On another board, someone believed it was failing because the page was timing out:

"...may fail, in the sense of timing as well. If it is successful, ff would have no reason not to find the element. Do some more research on securing the good behavior or this line. In particular, you might have to implement onreadystatechange event handler. Get rid of other functionality for testing."

Does that make sense?

If not, and I have to use a server-side solution, do you know anyone who could help me? I've got no experience with this at all.

Thanks
 
Expanding on what I initially wrote -

apparently it can't find URL, because the loading isn't done.

"When you load("xxx.xml"), the objDOM is not really supporting getElementById. To retrieve the one node you have useful info, you can do it by using the definitely supported .getElementsByTagName which gives you a collection, zero-indexed up to the .length-1. From each element so bound to, you use again the .getAttribute("id") (case may be sensitive here) to identify the target element. Once this is done, you retrieve the info.

Once .asyn is set to false, the loaded to be returned true if you have a valid xml file. It is very strict here. If anything wrong in the xml would fail all the following operations."

Does that make any sense?
 
I'll have a look it to it

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Someone else had this to say about the JS:

"It's pretty obvious why your javascript doesn'rt work - it uses Microsoft specifc code that will only work with IE on Windows! You shouldn't have any kind of XML stuff going on in your javascript - that ought to be all back up at the server. (Firefox will display XML out of
the box without any extra help of course)"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top