File: schemaexample.adb

package info (click to toggle)
libxmlada 25.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,212 kB
  • sloc: ada: 78,068; sh: 3,310; makefile: 394; xml: 111; python: 39
file content (69 lines) | stat: -rw-r--r-- 2,140 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
--
--  Copyright (C) 2017, AdaCore
--

with Input_Sources.Strings; use Input_Sources.Strings;
with Unicode.CES;           use Unicode.CES;
with Unicode.CES.Utf8;

with GNAT.IO;            use GNAT.IO;
with Sax.Readers;        use Sax.Readers;
with Schema.Readers;     use Schema.Readers;
with Schema.Schema_Readers; use Schema.Schema_Readers;
with Schema.Validators;  use Schema.Validators;

procedure SchemaExample is
   Input, Schema_Input : String_Input;
   My_Reader : Validating_Reader;

   Grammar           : XML_Grammar;
   Schema_From_String  : Schema_Reader;

   XML_Source_String : constant Unicode.CES.Byte_Sequence :=
     "<?xml version=""1.0"" ?>"
     & "<preferences>"
     & "<pref name=""pref1"">Value1</pref>"
     & "<pref name=""pref2"">Value2</pref>"
     & "</preferences> ";

   XSD_Source_String : constant Unicode.CES.Byte_Sequence :=
     "<?xml version=""1.0"" ?>"
     & "<xsd:schema xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">"
     & "  <xsd:element name=""preferences"">"
     & "    <xsd:complexType>"
     & "      <xsd:sequence>"
     & "         <xsd:element ref=""pref"" minOccurs=""1"" "
     & "maxOccurs=""unbounded""/>"
     & "      </xsd:sequence>"
     & "    </xsd:complexType>"
     & "  </xsd:element>"
     & "  <xsd:element name=""pref"">"
     & "    <xsd:complexType mixed=""true"">"
     & "       <xsd:attribute name=""name"" use=""required"" "
     & "type=""xsd:string"" />"
     & "    </xsd:complexType>"
     & "  </xsd:element>"
     & "</xsd:schema>";
begin
   Open (XSD_Source_String, Unicode.CES.Utf8.Utf8_Encoding, Schema_Input);
   Parse (Schema_From_String, Schema_Input);
   Close (Schema_Input);

   Grammar := Get_Grammar (Schema_From_String);

   Set_Public_Id (Input, "Preferences file");
   Open (XML_Source_String, Unicode.CES.Utf8.Utf8_Encoding, Input);

   Set_Feature (My_Reader, Schema_Validation_Feature, True);
   My_Reader.Set_Grammar (Grammar);

   Parse (My_Reader, Input);

   Close (Input);

   Put_Line ("OK");

exception
   when Schema.Validators.XML_Validation_Error =>
      Put_Line ("ERROR: " & Get_Error_Message (My_Reader));
end SchemaExample;