User Tools

Site Tools


k27:k27.8:start

27.8 Excursus: XSD Schema

Data can be packed and passed on in 1000 and one way. But only XML and XSD schema form the duo with which you can pass on valid data and also get back the processed data as valid data. In a schema - written in XML - the structure of the data is described in the corresponding XML document and statements on the data types including the value range are noted. This is not offered by other data formats - especially since the DOM allows you to easily edit (let) the data? read all data, read out a certain subset of data via filters, change, delete and insert data in elements and attributes. Linus Thorvalds is also to be understood in this direction if he remarks in an article:“Using XML only because it's chic is nonsense. XML only if you pass on data and let it be edited and the result is passed on again, etc.! To do this, however, you must insert a reference to a suitable XSD schema in an XML file in order to always process valid data.

There are two methods for checking the correct coding of XML files, which are fundamentally different from each other. One check is for whether one XML file is well-formed, while the other checks one XML file against an XSD schema.

  • Well-formedness - The syntax of the XML code must be correct.
  • Validity - If an XSD schema has been assigned to the XML file in a *. xsd file, the elements must follow the structure defined in the schema and the content must correspond to the data types of the individual elements and attributes defined in the schema. Every valid XML file is well-formed.

27.8.1 XSD scheme

An XSD schema:

  • describes which elements and attributes are contained in a document,
  • describes the data types for elements and attributes that can be simple or complex,
  • specifies the number and sequence of child elements (optional),
  • specifies restrictions for the text of elements and the values of attributes (optional) and
  • specifies the default and fixed values for elements and attributes (optional).

To check an XML file against a schema, you must create a link between the XML file and the external schema. This link is defined in the root element of the XML file. The link inserts the content of the XSD schema into the XML document:

<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">

27.8.2 Creating an XSD Schema

An XSD schema is always created on the basis of an existing XML file. XSD schema generators on the Internet offer a simple but convenient way to do this, since you can then also specify the XSD design:

https://www.freeformatter.com/xsd-generator.html

27.8.3 Check an XML file against an XSD schema

To check an XML file against an XSD schema, you can use suitable XML editors or XML validators (https://www.freeformatter.com/xml-validator-xsd.html). The author has had good experiences with the XML editor' XMLCopyEditor' and the console program' xmllint'. To install the named XML editor from a console:

$ sudo apt-get install xmlcopyeditor

The console program xmllint is part of libxml2-utils and is quickly installed:

$ sudo apt-get install libxml2-utils

To check the validity (validity) of an XML file, you need a suitable XSD schema. The two files test. xml and test. xsd - the contents of which you can read in the last section - are used for all further checks.

Checking in the XMLCopyEditor

OK

Figure 27.8.3.1: Successful check of the syntax of the XML file

The XML file test. xml is valid:

Figure 27.8.3.2: Successful check of the XML file against the XSD schema test. xsd

Check with the xmllint program

If a valid XML file is available, this is briefly and succinctly communicated:

$ xmllint --noout --schema test.xsd test.xml 
test.xml validates

On the other hand, if the check is negative, you can read a lot more, because there are also notes on the error in the XML file:

$ xmllint --noout --schema test.xsd test.xml 
test.xml:17: element geschlecht: Schemas validity error : Element 'geschlecht': [facet 'pattern'] The value 'W' is not accepted by the pattern 'm|w'. 
test.xml:17: element geschlecht: Schemas validity error : Element 'geschlecht': 'W' is not a valid value of the local atomic type. 
test.xml fails to validate

27.8.4 Gambas Project

In a Gambas project, an XML file is checked against an XSD schema. The xmllint program is used internally for the check in a shell instruction:

Public Sub btnValidateXML_Click()
 
  Dim sResult, sCommand As String
 
  sCommand = "xmllint --noout --schema " & File.SetExt($sXMLFilePath, "xsd") 
  sCommand &= " " & $sXMLFilePath & " 2>&1"  
  If Exist($sXMLFilePath) Then
     Shell sCommand To sResult
  Endif  
  If sResult Like "*validates*" Then
     Message.Title = "Prüfergebnis XML <<~>> XSD"
     Message.Info("Die XML-Datei '" & File.Name($sXMLFilePath) & "' ist gültig!")
  Else
     txaOriginal.Background = &HFF9F9F
     txaOriginal.Text = "\n" "FEHLER:" & "\n\n" & sResult
     btnValidateXML.Enabled = False
     txaOriginal.Pos = 0
  Endif    
End

These are the possible test results:

OK

Figure 27.8.3.3.3: Positive test result

 FEHLER

Figure 27.8.3.4: Negative test result

27.8.5 Contents of the files test. xml and test. xsd

File test. xml:

<?xml version="1.0" encoding="UTF-8"?> 
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd"> 
  <person id="1"> 
    <vorname>Peter</vorname> 
    <nachname>Mayer</nachname> 
    <adresse> 
      <strasse>Waldweg 4</strasse> 
      <ort plz="03866" ort="Querbach"></ort> 
    </adresse> 
    <geburtstag>1978-11-07</geburtstag> 
    <kommunikation> 
      <festnetz>0367587342</festnetz> 
      <mobil>017267339088</mobil> 
      <email>peter.mayer@web.de</email> 
      <web>www.gambasc.de</web> 
    </kommunikation> 
    <geschlecht>m</geschlecht> 
    <vl>false</vl> 
    <abteilung>A2</abteilung> 
    <zuzahlung>123.55</zuzahlung> 
  </person> 
  <person id="2"> 
    <adresse> 
    <vorname>Julie</vorname>    
    <nachname>O'Bryan</nachname> 
      <strasse>Querstrasse 22a</strasse> 
      <ort plz="07381" ort="Pößneck"></ort> 
    </adresse> 
    <geburtstag>1982-09-17</geburtstag> 
    <kommunikation> 
      <festnetz>0438776542</festnetz> 
      <mobil>01599099088</mobil> 
      <email>js_obryan@aol.com</email> 
      <web></web> 
    </kommunikation>    
    <geschlecht>w</geschlecht> 
    <vl>true</vl> 
    <abteilung>B1</abteilung> 
    <zuzahlung>92</zuzahlung> 
  </person>    
</data>

file test. xsd:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

<!-- 0. Ebene: Deklaration Root 'data' --> 
<xs:element name="data"> 
  <xs:complexType> 
    <xs:sequence> 
      <xs:element ref="person" minOccurs="0" maxOccurs="unbounded"/> 
    </xs:sequence> 
  </xs:complexType> 
</xs:element> 

<!-- 1. Ebene: Deklaration der Datensätze vom Typ 'person' --> 
<xs:element name="person"> 
  <xs:complexType> 
    <xs:sequence> 
      <xs:element ref="vorname" minOccurs="0"/> 
      <xs:element ref="nachname" minOccurs="0"/> 
      <xs:element ref="adresse"/> 
      <xs:element ref="geburtstag"/> 
      <xs:element ref="kommunikation"/> 
      <xs:element ref="geschlecht"/> 
      <xs:element ref="vl"/> 
      <xs:element ref="abteilung"/> 
      <xs:element ref="zuzahlung"/> 
    </xs:sequence> 
    <xs:attribute type="xs:positiveInteger" name="id" use="optional"/> 
  </xs:complexType> 
</xs:element> 

<!-- 2. Ebene: Deklaration aller komplexen Elemente --> 
<xs:element name="adresse"> 
  <xs:complexType> 
    <xs:sequence> 
      <xs:element ref="vorname" minOccurs="0"/> 
      <xs:element ref="nachname" minOccurs="0"/> 
      <xs:element ref="strasse"/> 
      <xs:element ref="ort"/> 
      </xs:sequence> 
   </xs:complexType> 
</xs:element> 

  <xs:element name="kommunikation"> 
    <xs:complexType> 
      <xs:sequence> 
        <xs:element ref="festnetz"/> 
        <xs:element ref="mobil"/> 
        <xs:element ref="email"/> 
        <xs:element ref="web"/> 
      </xs:sequence> 
    </xs:complexType> 
  </xs:element> 

<!-- 3. Ebene: Deklaration der einfachen Elemente  --> 
<xs:element name="ort"> 
  <xs:complexType> 
    <xs:simpleContent> 
      <xs:extension base="xs:string"> 
        <xs:attribute type="postleitzahl" name="plz" use="optional"/>
        <xs:attribute type="xs:string" name="ort" use="optional"/> 
      </xs:extension> 
    </xs:simpleContent> 
  </xs:complexType> 
</xs:element> 

<xs:element name="vorname" type="xs:string"/> 
<xs:element name="nachname" type="xs:string"/> 
<xs:element name="strasse" type="xs:string"/> 
<xs:element name="festnetz" type="de_national_telefon"/> 
<xs:element name="mobil" type="de_national_telefon"/> 
<xs:element name="web" type="xs:anyURI"/> 
<xs:element name="geburtstag" type="xs:date"/> 
<xs:element name="vl" type="xs:string"/> 
<xs:element name="zuzahlung" type="geldwert"/> 

<!-- Elemente mit Restriktionen --> 
<xs:element name="abteilung"> 
  <xs:simpleType> 
    <xs:restriction base="xs:string"> 
      <xs:pattern value="A1|A2|B1|B2|C1"/> 
    </xs:restriction> 
  </xs:simpleType> 
</xs:element> 

<xs:element name="geschlecht"> 
  <xs:simpleType> 
    <xs:restriction base="xs:string"> 
      <xs:pattern value="m|w"/> 
    </xs:restriction> 
  </xs:simpleType> 
</xs:element> 

<xs:element name="email"> 
  <xs:simpleType> 
    <xs:restriction base="xs:string"> 
    <!-- https://stackoverflow.com/questions/2147780/how-to-validate-an-email-id-in-xml-schema --> 
      <xs:pattern value="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}"/> 
    </xs:restriction> 
  </xs:simpleType> 
</xs:element> 

<!-- Eigene Datentypen --> 
<xs:simpleType name="geldwert"> 
  <xs:restriction base="xs:decimal"> 
  <xs:totalDigits value="5"/> 
  <xs:fractionDigits value="2"/> 
  </xs:restriction> 
</xs:simpleType> 

<xs:simpleType name="de_national_telefon"> 
    <xs:restriction base="xs:integer"> 
      <xs:pattern value="[0-9]*"/> 
    </xs:restriction> 
</xs:simpleType> 

<xs:simpleType name="postleitzahl">
    <xs:restriction base="xs:integer">
    <!-- <xs:pattern value="[0-9]{5}"/>  Trivialer Ansatz – erkennt PLZ 62345 nicht als fehlerhaft -->
    <!-- Zu den Ziffernkombinationen 00, 05, 43, 62 am Anfang ist keine Post-Leitregion definiert! -->
    <xs:pattern value="0[1-46-9][0-9]{3}|[1-357-9][0-9]{4}|4[0-24-9][0-9]|{3}|6[013-9][0-9]{3}"/>
    </xs:restriction>
</xs:simpleType>

</xs:schema>

The interaction between the XML file and the XSD file becomes well understandable if you enter some data incorrectly in the testing of the presented Gambas project and then check it:

PLZ             3960 		' Es fehlt eine Ziffer
PLZ             43966		' Diese PLZ existiert nicht, da die Post-Leitregion 43 nicht existiert
EMail           js_o'bryan@... 	' Unzulässiges Zeichen '
Abteilung       A4 		' Die Aufzählung enthält keine Abteilung A4
Datum           2015-31-12 	' Datumsformat nach ISO 8601:2004 (Erweiterte Syntax: YYYY-MM-DD) falsch
Telefonnummer   +49 03937868686	' Das Pluszeichen ist keine Ziffer - wie auch das Leerzeichen

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k27/k27.8/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools