2

Can anyone come up with the javascript needed to do this?

<?php
# START XSLT
$xslt = new XSLTProcessor(); 
$XSL = new DOMDocument(); 
$XSL->load('example.xsl'); 
$xslt->importStylesheet($XSL); 

# LOAD XML FILE 
$XML = new DOMDocument();
$XML->load('data.xml');

#PRINT 
print $xslt->transformToXML($XML); 
    ?>

Thanks, I'm using this to transform some XML data on my HTML page.

SheetJS
  • 22,470
  • 12
  • 65
  • 75
stockoverflow
  • 1,457
  • 5
  • 18
  • 22

2 Answers2

1

Add this line to your data.xml after the XML prolog:

<?xml-stylesheet type="text/xsl" href="example.xsl"?>

If your browser supports XSLT, it shouly apply the transformation and display the transformed result.

If you want to do this with JavaScript, have a look at these:

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • @Gordon So I don't actually need any Javascript? I currently have a HTML file (fancy header, footer and all) and I want the content section to display those XML/XSL files sitting on the same directory. – stockoverflow Apr 30 '11 at 12:34
  • I've done this [here](http://stackoverflow.com/questions/5771106/trouble-displaying-xml-file-styled-by-xslt-on-chrome-browser-closed), but it doesn't seem like Chrome allows this for local files (which is what I'm working on). – stockoverflow Apr 30 '11 at 12:45
  • @stock technically no. You can assemble the entire output from the xslt, including headers, footers and stuff. But of course, it boils down to browser support. If Chrome cannot do that, it cannot do that. The question then is: can it do that from JavaScript. I doubt that, but I really dont know. – Gordon Apr 30 '11 at 12:46
  • @gordon appreciate the advice. I'm trying to make simple message board using dropbox... would storing all the post data in XML be a reasonable approach? – stockoverflow Apr 30 '11 at 12:54
  • @stock that depends on whether the amount of required disk i/o is acceptable to the amount of requests you intend to serve. – Gordon Apr 30 '11 at 12:56
  • @gordon it's just a small group of 4 people, average of 3 posts per day. I should have asked first... is it possible? I'm not really a programmer but the process I'd imagine would be: 1. Fill out HTML form, 2. Submit, 3. JavaScript creates new nodes/elements on the XML from submitted data. – stockoverflow Apr 30 '11 at 13:02
  • @stock but how you are going to make the XML available to those four people then? Usually, those forms are filled in on the client side, e.g. the people's computer. Then the form is submitted to some server. The server puts it into the XML and then delivers a copy of that to the people's computer again. If you do that all locally on the people's computers how would they see the updates? Unless you are using server side JavaScript, PHP is the better choice to handle the postings. – Gordon Apr 30 '11 at 13:16
  • @gordon I believe dropbox syncs the XML file to the others when it's edited. There is no actual server, just relying on dropbox's sync. – stockoverflow Apr 30 '11 at 13:38
  • @stock I see. Sorry, cant help with that. Am not familiar with dropbox – Gordon Apr 30 '11 at 13:46
1

Using a processing instruction as suggested by Gordon might do the job for you. If you need more control, you might want to take a look at the Sarissa JavaScript library, which promises to do XML processing in a reliable fashion across browsers.

Lumi
  • 14,775
  • 8
  • 59
  • 92
  • Sarissa is also suggested in my second link, but I never used it. Do you know if it actually lives up to it's promise? – Gordon Apr 30 '11 at 09:37
  • No, I don't. I saw knowledgeable people recommend it about two years ago but have never tried using it myself. – Lumi Apr 30 '11 at 09:56
  • I used sarissa.js and it only worked for FF4. Chrome, IE9, Safari and Opera didn't display. But sarissa was very easy to use for someone who didn't know any JS. – stockoverflow Apr 30 '11 at 13:21