1

I've been tasked with taking a sample of survey results that are stored in CSV and to convert them to XML.

CSV Data

I'm still fairly new to XML, but have come up with a couple of layouts I could use and was hoping I could get some advice on what way is best to go.

1)

<survey>
    <response>
        <id>1</id>
        <question>
            <title>When did you start playing Pokemon Go?</title>
            <answer>June</answer>
        </question>
        <question>
            <title>What type of phone/OS do you play on?</title>
            <answer>Android</answer>
        </question>
        <question>
            <title>What team are you on?</title>
            <answer>Mystic (Blue)</answer>
        </question>
        <question>
            <title>What Level Are You?</title>
            <answer>29</answer>
        </question>
        <question>
            <title>How many pokemon have you caught?</title>
            <answer>0 to 1000</answer>
        </question>
        <question>
            <title>How many KM have you traveled? (Jogger medal)</title>
            <answer>Bronze</answer>
        </question>
    </response>
    ... more responses ...
</survey>

2)

<survey>
    <response id="1">
        <answer question="1">June</answer>
        <answer question="2">Android</answer>
        <answer question="3">Mystic (Blue)</answer>
        <answer question="4">29</answer>
        <answer question="5">0 to 1000</answer>
        <answer question="6">Bronze</answer>
    </response>
    ... more responses ...
</survey>

Should I include the question text at all, since it will be repeated for every response?

I will be using a XSD file to put restrictions on what the answers can be and then transforming it using an XSLT to display the data in charts/tables.

1 Answer 1

4

Which is the better encoding really depends on what you're trying to store. If you're storing the data gathered by the survey, it'll be much different than if you're storing a record of the user input from the survey.


If you're storing the record of user input, something like this may be appropriate:

<survey>
  <questions>
    <question id="1">When did you start playing Pokemon Go?</question>
    <question id="2">What type of phone/OS do you play on?</question>
    <question id="3">What team are you on?</question>
    <question id="4">What Level Are You?</question>
    <question id="5">How many pokemon have you caught?</question>
    <question id="6">How many KM have you traveled? (Jogger medal)</question>
  </questions>
  <responses>
    <response id="1">
        <answer question="1">June</answer>
        <answer question="2">Android</answer>
        <answer question="3">Mystic (Blue)</answer>
        <answer question="4">29</answer>
        <answer question="5">0 to 1000</answer>
        <answer question="6">Bronze</answer>
    </response>
    ... more responses ...
  </responses>
</survey>

The important thing here is to include enough information to reconstruct the survey. If the answers aren't free-form, it might be worth including the available responses in the question block, possibly like this:

<survey>
  <questions>
    ...
    <question id="2" text="What type of phone/OS do you play on?">
      <option id="1">iOS</option>   
      <option id="2">Android</option>
      <option id="3">Other</option>
    </question>
    ...
  </questions>
  <responses>
    <response id="1">
        ...
        <answer question="2" response="1"/>
        ...
    </response>
    <response id="2">
        ...
        <answer question="2" response="3">MS Surface</answer>
        ...
    </response>
    ... more responses ...
  </responses>
</survey>

If you're just storing the collected data, it can be much simpler (basically just providing by name access to the fields):

<player-data>
  <player id="1"
          started="June"
          platform="Android"
          team="Mystic (Blue)"
          level="29"
          catchCount="0 to 1000"
          joggerMedal="Bronze"/>
  ... more players ...
</player-data>
4
  • 1
    For the purposes of what I'm being asked, i think the first option with added questions element would be the best. Thanks for your input!
    – screencut
    Apr 7, 2018 at 19:04
  • Glad to help :), though you may want to wait the customary 24 hours before marking an accepted answer, as there may be a better encoding waiting in the wings
    – Morgen
    Apr 7, 2018 at 19:11
  • 1
    The first one seems to me the more flexible and resilient. The 3rd has an odd semantic. On the other hand, encoding free text into XML attributes can be painful when it comes to encoding special characters like quotes or double quotes. The key around output formats is to ease the reading. Which one is easy to read or easier to implement a reader. So please, don't do things like: <questions><1></1><2></2></questions>
    – Laiv
    Apr 9, 2018 at 7:09
  • 1
    Methods 1 and 2 seem fine (and +1 for naming the "question" attributes that, instead of calling them "id" -- they are idrefs in XML terms, not IDs). Whether some or all questions are multiple-choice vs. written-in answers is something to consider. I'd avoid method 3, because you'd have to change the schema each time you change the survey, or do a new survey. Though it is nicely compact....
    – TextGeek
    Apr 13, 2022 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.