====== Creating Questionnaires as XML Files (Guide for AI Assistants) ====== This page is meant for AI assistants (ChatGPT, Claude, Gemini, ...) that create a file for SoSci Survey users, which the users then import into their survey project. The file contains questions (one section in the list of questions) and, optionally, a questionnaire that arranges these questions on pages. Related pages: * [[:en:ai|SoSci Survey Developer Documentation for LLMs]]: Content structure, variables, forbidden PHP functions. * [[:en:ai-xml-types|Question Types for the XML Import (Reference)]]: All question types with their attributes, allowed values and defaults. * [[:en:create:functions:ai_context|SoSci Survey PHP Sandbox Context]]: What is allowed in PHP code within the questionnaire. * [[:de:ai-faq|SoSci Survey FAQ für KI-Assistenten]] (German): Frequently asked questions and ground rules for answers. * [[https://www.soscisurvey.de/templates/doctype.survey.dtd|Document Type Definition (DTD)]]: The formal structure of the file. ===== Procedure ===== - Clarify the content with the user: questions, response options, scales, order, filters. - Clarify the languages: In which language are the texts (base language)? Shall there be translations? - Create the file according to the rules below and output it completely (one code block, file extension ''.xml''). - The user saves the file and imports it in SoSci Survey via **List of Questions** → **New section** → //Import Questions or Other Content from an XML File//. During the import, SoSci Survey asks for the ID of the new section and the name of the questionnaire. Existing contents of the project are kept. - If the import reports errors or warnings, ask the user for the text of the messages and correct the file. ===== Complete Example ===== The example contains a section "LI" with a welcome text and three questions (selection, scale, open text input), English texts with a German translation, and a questionnaire with two pages and a filter. SoSci Survey 4.0.03 2026-11-25 12:00:00 Library Satisfaction Short questionnaire on library use and satisfaction eng eng ger
Library LI Welcome TextContent <explanation /> <headerText> <txt>Welcome</txt> <txt lng="ger">Willkommen</txt> </headerText> <lead> <txt><![CDATA[Thank you for taking part in our survey. It takes about 3 minutes.]]></txt> <txt lng="ger"><![CDATA[Vielen Dank, dass Sie an unserer Befragung teilnehmen. Die Beantwortung dauert etwa 3 Minuten.]]></txt> </lead> <attributes.specific program="ofb"> <attr id="id">1</attr> </attributes.specific> </question> <question> <description>Frequency of use</description> <class>select</class> <title> <txt>How often do you use the library?</txt> <txt lng="ger">Wie oft nutzen Sie die Bibliothek?</txt> 2 yes several times a week mehrmals pro Woche 1 about once a week etwa einmal pro Woche 2 less often seltener 3 never nie 4 Satisfaction scale <txt>To what extent do you agree with the following statements?</txt> <txt lng="ger">Inwieweit stimmen Sie den folgenden Aussagen zu?</txt>
  • strongly disagree stimme überhaupt nicht zu
  • strongly agree stimme voll und ganz zu
  • don't know weiß nicht
  • 3 5 I find the books I am looking for. Ich finde die Bücher, die ich suche. 1 The opening hours suit my daily life. Die Öffnungszeiten passen zu meinem Alltag. 2 It is often too noisy for me. Es ist mir oft zu laut. yes 3
    Reasons for not using text <txt>Why don't you use the library?</txt> <txt lng="ger">Warum nutzen Sie die Bibliothek nicht?</txt> 4 1 120
    Library Survey library <?xml version="1.0"?> <questionnaire> <page> <question id="LI01" /> <question id="LI02" /> </page> <page> <php><![CDATA[ if (value('LI02') == 4) { question('LI04'); } else { question('LI03'); } ]]></php> </page> </questionnaire>
    ===== File Structure ===== ==== Header ==== * The file starts with ''%%%%'' and the ''%%%%'' line as in the example. The file must be UTF-8 encoded. * The root element ''%%%%'' contains, in this order: ''%%%%'', ''%%%%'', ''%%%%'', ''%%%%'', ''%%<description>%%'', ''%%<language>%%'', ''%%<attributes.specific>%%'' (languages), exactly one ''%%<section>%%'' and optionally exactly one ''%%<questionnaire>%%''. * ''%%<title>%%'' and ''%%<description>%%'' describe the file. SoSci Survey suggests the name of the questionnaire based on the title during the import. * For several sections, create several files – or a project file (see below). ==== Languages ==== * Languages are given as three-letter codes, e.g. ''eng'' (English), ''ger'' (German), ''fre'' (French), ''spa'' (Spanish), ''ita'' (Italian). * ''%%<language>%%'' and ''%%<attr id="language">%%'' state the base language, ''%%<attr id="translations">%%'' the translations (several separated by commas). Without translations, ''%%<attr id="translations">%%'' is omitted. * A translatable text contains one ''%%<txt>%%'' without ''lng'' for the base language and one ''%%<txt lng="...">%%'' per translation. Without translations, the text may also be written directly in the element: ''%%<title>How old are you?%%''. * During the import, the user can choose whether to apply the language settings. If not, texts in languages that the project does not use are not imported. ==== Section and IDs ==== * The section (''%%
    %%'') has a title and an ID of two capital letters (''%%LI%%''). The user may choose another ID during the import; references in the questionnaire are then adjusted automatically. * Each question has a number from 1 to 99 (''%%%%''). It makes up the question ID: section + two-digit number, e.g. ''LI02''. This ID is used in the questionnaire and in PHP code. * Each item has a number (''%%%%''), which also is the response code in selection questions. The variable of an item is named e.g. ''%%LI03_01%%'' (question LI03, item 1). In selection questions (''select'', ''dropdown''), the variable of the question (''LI02'') stores the code of the selected option. * The positions (''%%%%'') may be omitted; then the order in the file applies. ==== Questions ==== * A ''%%%%'' element always needs ''%%%%'' (internal description, not visible to respondents), ''%%%%'' (the question type), ''%%%%'' (the question text) and ''%%<explanation>%%'' (instructions) – empty as ''%%<title />%%'' or ''%%<explanation />%%''. * The elements within ''%%<question>%%'' and ''%%<item>%%'' have a fixed order. The reference states this order, and which attributes are elements of their own and which are written as ''%%<attr id="...">%%'' in ''%%<attributes.specific program="ofb">%%''. * Only specify attributes that differ from the default value. Only use attributes and values that the reference lists for the respective question type; unknown attributes are ignored with a warning during the import. * Yes/no attributes have the values ''yes'' and ''no''. Widths and heights are given in pixels. Only specify a height for a text input if the field shall have several lines (at least 32 pixels). * Label scales (''scale'', ''gradual'', ''slider'', ''polar'') with ''%%<labels>%%'': ''i'' is the scale value, e.g. ''%%i="1"%%'' for the minimum and ''%%i="5"%%'' for the maximum of a 5-point scale (''%%<attr id="scale.niveau">5</attr>%%''). A residual option ("don't know") goes into ''%%<residualOptions>%%'' with ''%%i="0"%%'' and is stored with the code -1. * Texts for respondents may contain HTML. Enclose texts with ''%%<%%'', ''%%&%%'' or HTML tags in ''%%<![CDATA[ ... ]]>%%''. * Texts without a response (welcome text, notes, headings) are not questions but media elements of the type ''TextContent''. In the file, they are nevertheless written as ''%%<question>%%'' in the section and get an ID like a question: the heading in ''%%<headerText>%%'', the text in ''%%<lead>%%''. ==== Questionnaire ==== * ''%%<questionnaire>%%'' contains ''%%<title>%%'' and, in ''%%<attributes.specific>%%'', the name (''%%<attr id="id">%%'', up to 40 characters: letters without umlauts, digits, ''-'' and ''%%_%%'') and the content (''%%<attr id="content">%%''). * The content is an XML document itself, stored as **text** in the attribute. Write it as normal XML first, then escape it completely: ''%%&%%'' becomes ''%%&%%'', ''%%<%%'' becomes ''%%<%%'', ''%%>%%'' becomes ''%%>%%''. The content must start with ''%%<?xml version="1.0"?>%%'' (escaped: ''%%<?xml version="1.0"?>%%''). * The questionnaire consists of ''%%<page>%%'' elements in the order of the pages. A page contains: * ''%%<question id="LI02" />%%'' – a question from the list of questions * ''%%<php><![CDATA[ ... ]]></php>%%'' – PHP code, e.g. for filters (see below) * ''%%<html><![CDATA[ ... ]]></html>%%'' – HTML code that is placed directly on the page * The attributes ''intID'' (internal numbers) may be omitted; SoSci Survey adds them. * SoSci Survey adds the final page (end of the questionnaire) automatically, it is not specified. ==== Filters and PHP Code ==== * Filters are implemented as PHP code on the page where the questions are displayed. ''value()'' reads responses from previous pages, e.g. ''%%value('LI02')%%'' for the code of the selected option. ''%%question('LI04')%%'' displays a question, ''goToPage()'' jumps to another page. * PHP code is only executed when the respondent clicks "Next" and the next page is created. So a filter cannot react to responses on the same page; that requires JavaScript. * The PHP code runs in a sandbox. Only the functions from the manual are allowed; ''echo'', ''%%$_GET%%''/''%%$_POST%%'', global variables as well as file and database access are not possible. Details: [[:en:create:functions:ai_context|SoSci Survey PHP Sandbox Context]]. ===== Complete Project (Advanced) ===== A project file (root element ''%%<surveyProject>%%'') contains a complete project: several sections, several questionnaires, text elements and settings. Only use it if the user explicitly wants a complete project, e.g. for a new, empty project. * When importing a project file, SoSci Survey asks whether the previous contents of the project are deleted. Point this out to the user. * Structure: ''%%<surveyProject version="2.5" progversion="4.0.03">%%'' with ''%%<title>%%'', ''%%<description>%%'', ''%%<attributes.specific>%%'' (languages as above), followed by any number of ''%%<section>%%'' and ''%%<questionnaire>%%'' (order according to the DTD: ''section'' before ''questionnaire''). * Each section has its own ID and a position (''%%<attr id="pos">%%''). The IDs are not adjusted, so references in the questionnaire must match the IDs in the file. ===== Checklist Before Output ===== * Header, DOCTYPE and ''%%version="2.5"%%'' as in the example, file in UTF-8. * Each question has ''%%<description>%%'', ''%%<class>%%'', ''%%<title>%%'' and ''%%<explanation>%%'' in this order, the other elements in the order of the reference. * Question types and attributes are taken from the reference; values are allowed values. * Numbers of questions (1–99) and items are unique within their section or question. * Each ID used in the questionnaire (e.g. ''LI04'') exists in the section. * The questionnaire content is completely escaped and starts with ''%%<?xml version="1.0"?>%%''. * No HTML forms (''%%<form>%%'', ''%%<input>%%'') in texts: such inputs are not stored. Use the question types for inputs.