Reputation: 45
I've been trying to import an XML document into HTML without using a server. I'm working with standalone computers so I can't upload it to any server, which means I can't use XMLHttpRequest. I found a thread here saying I could use jQuery AJAX, but as it turns out, I can't use that either. Does anybody know a way I could do it without copying the entire XML into the JS code?
To requests, here's what I need to do with it: Basically the XML contains questions from which I want to create a test. Now, the test doesn't include all the questions, so I'm using JS to pick random questions and place them in the HTML document. I don't want to copy the entire XML to the script file because it looks really unprofessional and I want to keep it as simple as possible.
At this point I'm really willing to try anything, including JSON, if that makes it easier. I just need the ability to use JS on the data and the ability to write it in the HTML document.
Upvotes: 4
Views: 1617
Reputation: 1259
If you are simply trying to transform an XML document into HTML the shortest path is to use XSLT (EXtensible Stylesheet Language). This involves defining an additional XSLT document which describes how your XML should be transformed and the output can be HTML should you choose. One introduction to XSLT is here: http://www.w3schools.com/xsl/.
Most people will resort to JavaScript, but that's a deeper rabbit hole IMHO.
Upvotes: 0
Reputation: 63588
One option would be to include the question file as JavaScript content (still in a separate file) instead.
If the content is really simple (eg just a list) then your file could just be a simple array.
var questions = [
"What time is it?",
"Is the sky blue?",
"What do you call a fish with no legs?"
];
You just include this file in your page with a script tag:
<script src="questions.js"></script>
If your question structure is more complicated you can include it in a JSON format that allows for complex nesting etc.
var questions = [
{
"id":1,
"question":"What time is it?",
"datatype":"text"
},
{
"id":2,
"question":"Is the sky blue?",
"datatype":"boolean"
},
{
"id":3,
"question":"What do you call a fish with no legs?",
"datatype":"text"
}
];
Either way once the question data is loaded into memory you can randomize the questions and decide which ones to render.
Upvotes: 1
Reputation: 739
Well, you can run a local server on your stand alone computer. There are many to choose from, just google. That is what localhost is for. ie http://127.0.0.1/some_xml_file.xml
Upvotes: 0