您在這裡

DTD documents and XML schema

21 一月, 2016 - 14:46

As it was shown, the validity of XML document can be checked by a lexical analyzer. But in terms of the W3C organization (XML Essentials) valid XML document is a well formed XML document, which also conforms to the rules of a Document Type Definition (DTD) or by its successor – XML Schema.

media/image19.png
Figure 2.19 Defining a new word for synonyms window 

The DTD may be declared inside the XML file (then it should be wrapped in a DOCTYPE definition) or in an external file (then, XML file should be assigned to the DTD definition by <!DOCTYPE root-element SYSTEM "DTDfilename"> element). There is the possibility to extend MOODLE platform with required DTD or XML Schema validation functionality (Webmaster Tutorial – PHP Tutorials), however such validators are also available on-line. As an example, http://www.w3schools.com/xml/xml_validator.asp web page is presented in (Figure 2.20).

media/image20.png
Figure 2.20 The sample of two-step XML Schema validation window 
 

In addition to XML instance against DTD/XML Schema validation, another important problem was solved in the described XML module. In order to provide mechanisms for comprehensive study of the rules regarding constructing of DTD and XML Schema documents, the suitable algorithms were implemented.

For example, one of the module tasks is to study the sample XML instance, written in a task body and correct given DTD document to fit the specified XML. The mechanism which had to be implemented in the MOODLE platform to check the coincidence of learners’ responses with the “model” answer defined by a teacher, was coded in PHP by the usage of auxiliary files.

To grade the correctness of trainee solution (Figure 2.21), the sample (i.e. accurate), well-formed, DTD document defined by a teacher is uploaded into a supplementary text file, while the sent answer - into the second temporary text file.

media/image21.png
Figure 2.21 The DTD correctness task window 
 

Both files are stored in the MOODLE backend server. Because of the fact, a lot of course participants can execute the task at the same time, the temporary file name generated for a particular MOODLE user solution is dynamically composed in a following manner:

XML_UserId_ModuleId_TaskNo.txt.

UserId, ModuleId and TaskNo represent course participant, module and task unique identifiers respectively and are accessible through PHP variables $XML->dblogin, $course->id, $XML->id, where $XML = get_record('XML', 'id', $cm->instance). Additionally, to avoid the situation, the numerous spaces will influence the solutions discrepancy, the undue spaces and whitespaces are eliminated from sent answer by the use of regular expressions in form of preg_replace("@\s+@"," ",$file). Generally, the whole algorithm is as follows:

Set variables, pointing to both files;

Open files for reading;

Get the contents of both files;

Remove undue spaces and whitespaces from the file contents;

For (each DTD entry) do {

Compare an entry with its exemplar;

If (the entries are the same) {

Return the message of successful task solving;

}

Else {

Return the message of incorrect task solving; }

}

Close the files;

Although the XML Schema defining rules differ from the DTD ones, the problems that had to be solved by providing the MOODLE environment with adequate mechanisms were similar. But it must be emphasized, that XML module tasks referring to XML Schemas play another, crucial, role in the topic of storing XML in relational database. Thus, numerous of the new XML functionalities were formulated with regard to a database used in this scope.