Importing XML to HTML Without a Server

后端 未结 3 1330
深忆病人
深忆病人 2021-01-25 15:24

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 us

相关标签:
3条回答
  • 2021-01-25 15:30

    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.

    0 讨论(0)
  • 2021-01-25 15:38

    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.

    0 讨论(0)
  • 2021-01-25 15:45

    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

    0 讨论(0)
提交回复
热议问题