34
35:- module(xsdp_type,
36 [ xsdp_type/1, 37 xsdp_uri_type/2, 38 xsdp_numeric_uri/2, 39 xsdp_subtype_of/2, 40 xsdp_convert/3 41 ]). 42:- autoload(library(sgml), [xsd_number_string/2]).
63ns('http://www.w3.org/2001/XMLSchema#').
64
65
66
74xsdp_type(Type) :-
75 subtype_of(Type, _).
81xsdp_uri_type(URI, Type) :-
82 xsd_local_id(URI, Type),
83 subtype_of(Type, _).
89xsdp_subtype_of(Type, Type) :-
90 subtype_of(Type, _).
91xsdp_subtype_of(Type, Super) :-
92 ( nonvar(Type)
93 -> subtype_of(Type, Super0),
94 Super0 \== (-),
95 xsdp_subtype_of(Super0, Super)
96 ; nonvar(Super)
97 -> subtype_of(Sub0, Super),
98 xsdp_subtype_of(Type, Sub0)
99 ; subtype_of(Type, _),
100 xsdp_subtype_of(Type, Super)
101 ).
102
103subtype_of(anyType, -).
104subtype_of(anySimpleType, anyType).
105 106subtype_of(string, anySimpleType).
107subtype_of(normalizedString, string).
108subtype_of(token, normalizedString).
109subtype_of(language, token).
110subtype_of('Name', token).
111subtype_of('NCName', 'Name').
112subtype_of('NMTOKEN', token).
113subtype_of('NMTOKENS', 'NMTOKEN').
114subtype_of('ID', 'NCName').
115subtype_of('IDREF', 'NCName').
116subtype_of('IDREFS', 'IDREF').
117subtype_of('ENTITY', 'NCName').
118subtype_of('ENTITIES', 'ENTITY').
119 120subtype_of(decimal, anySimpleType).
121subtype_of(integer, decimal).
122subtype_of(nonPositiveInteger, integer).
123subtype_of(negativeInteger, nonPositiveInteger).
124subtype_of(long, integer).
125subtype_of(int, long).
126subtype_of(short, int).
127subtype_of(byte, short).
128subtype_of(nonNegativeInteger, integer).
129subtype_of(unsignedLong, nonNegativeInteger).
130subtype_of(positiveInteger, nonNegativeInteger).
131subtype_of(unsignedInt, unsignedLong).
132subtype_of(unsignedShort, unsignedInt).
133subtype_of(unsignedByte, unsignedShort).
134 135subtype_of(duration, anySimpleType).
136subtype_of(dateTime, anySimpleType).
137subtype_of(time, anySimpleType).
138subtype_of(date, anySimpleType).
139subtype_of(gYearMonth, anySimpleType).
140subtype_of(gYear, anySimpleType).
141subtype_of(gMonthDay, anySimpleType).
142subtype_of(gDay, anySimpleType).
143subtype_of(gMonth, anySimpleType).
144subtype_of(boolean, anySimpleType).
145subtype_of(base64Binary, anySimpleType).
146subtype_of(hexBinary, anySimpleType).
147subtype_of(float, anySimpleType).
148subtype_of(double, anySimpleType).
149subtype_of(anyURI, anySimpleType).
150subtype_of('QName', anySimpleType).
151subtype_of('NOTATION', anySimpleType).
159term_expansion(integer_types, Clauses) :-
160 findall(integer_type(Type), xsdp_subtype_of(Type, integer), Clauses).
161term_expansion(xsd_local_ids, Clauses) :-
162 ns(NS),
163 findall(xsd_local_id(URI, Type),
164 ( xsdp_type(Type),
165 atom_concat(NS, Type, URI)
166 ),
167 Clauses).
168term_expansion(numeric_uirs, Clauses) :-
169 findall(xsdp_numeric_uri(URI, PrimaryURI),
170 ( ( integer_type(Type), Primary = integer
171 ; Type = float, Primary = float
172 ; Type = double, Primary = double
173 ; Type = decimal, Primary = decimal
174 ),
175 xsd_local_id(URI, Type),
176 xsd_local_id(PrimaryURI, Primary)
177 ),
178 Clauses).
179
180integer_types.
181xsd_local_ids.
182
183numeric_uirs.
190xsdp_convert(URI, Content, Value) :-
191 ( xsd_local_id(URI, Type)
192 -> convert(Type, Content, Value)
193 ; convert(URI, Content, Value)
194 ).
195
196convert(anyType, Term, Term) :- !.
197convert(anySimpleType, [Simple], Simple) :- !.
198 199convert(string, [String], String) :- !.
200 201convert(IntType, [Text], Integer) :-
202 integer_type(IntType),
203 !,
204 ( xsd_number(Text, Integer),
205 integer(Integer),
206 validate_int_domain(IntType, Integer)
207 -> true
208 ; throw(error(domain_error(IntType, Text), _))
209 ).
210convert(float, [Text], Float) :-
211 !,
212 xsd_float(Text, float, Float).
213convert(double, [Text], Float) :-
214 !,
215 xsd_float(Text, double, Float).
216convert(_Any, [X], X) :- !. 217convert(_Any, X, X).
218
223
224xsd_number(Text, Number) :-
225 split_string(Text, "", " \t\r\n", [Trimmed]),
226 catch(xsd_number_string(Number, Trimmed), error(syntax_error(_), _), fail).
227
228xsd_float(Text, Type, Float) :-
229 ( xsd_number(Text, Number)
230 -> Float is float(Number)
231 ; throw(error(domain_error(Type, Text), _))
232 ).
233
234validate_int_domain(integer, _).
235validate_int_domain(int, I) :- between(-2147483648, 2147483647, I).
236validate_int_domain(long, I) :- between(-9223372036854775808,
237 9223372036854775807, I).
238validate_int_domain(nonPositiveInteger, I) :- \+ I > 0.
239validate_int_domain(negativeInteger, I) :- I < 0.
240validate_int_domain(short, I) :- between(-32768, 32767, I).
241validate_int_domain(byte, I) :- between(-128, 127, I).
242validate_int_domain(nonNegativeInteger, I) :- \+ I < 0.
243validate_int_domain(unsignedLong, I) :- between(0, 18446744073709551615, I).
244validate_int_domain(positiveInteger, I) :- I > 0.
245validate_int_domain(unsignedInt, I) :- between(0, 4294967295, I).
246validate_int_domain(unsignedShort, I) :- between(0, 65535, I).
247validate_int_domain(unsignedByte, I) :- between(0, 255, I)
XML-Schema primitive types
This modules provides support for the primitive XML-Schema (XSD) datatypes. It defines the type hierarchy which allows for reasoning over types as well as xsdp_convert/3 to convert XML content to a natural Prolog representation of the XSD type.
Based on the W3C definitions at
The current implementation is incomplete and only there to test the API and its integration with rdf:dataType=Type handling in the RDF parser.
The extra 'p' in the module prefix (xsdp_*) is used to allow for a module xsd_*, providing full user-defined XSD types on top of this module.