## Datatyping in RDF/XML

### Introduction

The original RDF Model and Syntax specification states a property value (i.e. the object of a triple) may be denoted by any of the following:

1. Literal (a simple untyped string that is self-denoting)
2. Blank node (also known as anonymous nodes)
3. A URI Reference per (RFC2396)
4. XML literal (this is a special case of Literal; it is supported syntactically via the rdf:parseType="Literal" attribute/value pair)

Note, however, the Model and Syntax specification and the (2000-03-27) RDF Schema Candidate Recommendation do not provide any direct support for datatyping - that is, they do not define a way to denote datatype values (i.e. no mechanism to state lexical constraints for a property's values). The only loosely-related mechanisms are the rdfs:range and rdfs:domain properties but they are designed to place rdfs:Class constraints on a property and do not provide any semantics for lexical constraints. Note the rdfs:range of rdfs:range is rdfs:Class and not a datatype.

### Datatyping Support in RDF

To facilitate datatyping in RDF, the new RDF specifications (see More Information) allow a property value to be a typed literal. A property has a typed literal value if and only if the property element in the RDF/XML has a rdf:datatype attribute. For example a property for a person's age could have a rdf:datatype attribute with a value of something like xsd:nonNegativeInteger. Thus with the new specifications, a property's value may be denoted by any of the values listed above but it may also be denoted by the following:

5. A typed literal (denotes a datatype value)

Additionally, the Model and Syntax specification is unclear about whether a rdfs:Literal is a sub-class of a rdfs:Resource. The new specifications have clarified this relationship: rdfs:Literal is a sub-class of rdfs:Resource. Consequently, a property's value is always a resource.

### Key Concepts

Note the following key concepts regarding datatyping and RDF:

- RDF has no built-in or native datatypes of its own. Thus RDF does not define datatypes for abstractions such as integers or real numbers. One exception, however, is the new rdf:XMLLiteral datatype (defined for XML literal property values).
- RDF does not define any mechanisms for creating a datatype.
- RDF Schema's rdfs:Datatype class (described below) is used to declare (not create) a datatype.
- RDF provides a mechanism by which a datatype is associated with a lexical form. In RDF/XML this is done by placing a rdf:datatype attribute on the property element enclosing the lexical form.
- RDF defines no mechanism for verifying that the value of a rdf:datatype attribute actually denotes a datatype. However, the use of the rdf:datatype attributes constitutes an (implicit) assertion that the resource denoted by the attribute value is in fact a member of rdfs:Datatype.
- RDF does not state how an application should verify that the lexical form (the data content of the property element) is a valid lexical form for the datatype specified by the rdf:datatype attribute and hence represents an actual datatype value.
- Although XML Schema is used illustratively in the RDF specifications, datatypes from other systems - which conform to the characteristics defined for instances of rdfs:Datatype - may be used.
- Since a datatype from any system may be used, a processor may encounter datatypes it does not support (i.e. the semantics and/or lexical space for the datatype are not known).

### Using XML Schema Datatypes in RDF

The following fragment of RDF/XML shows the usage of XML Schema's nonNegativeInteger datatype for the ex:age property:

     <rdf:Description rdf:about="http://www.example.org/index.html">
       <ex:age rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">23</ex:age>
     </rdf:Description>

There are several important things to note regarding using XML Schema datatypes in RDF/XML:

- The base URI http://www.w3.org/2001/XMLSchema must be used (per XML Schema's namespace considerations) to avoid interoperability problems.
- The value of a rdf:datatype attribute must be a RDF URI Reference for the datatype.
- If an application must operate on a datatype value for a typed literal, the application is responsible for verifying the property value matches the lexical space defined by the datatype.

An application is free to define its own datatypes in any language(s). However, XML Schema is expected to be a popular choice for applications of RDF.

### Creating a Datatype Using XML Schema

The following XML Schema fragment defines two datatypes: the Boolean datatype is constrained to strings with the values Yes or No; the Dimension datatype is constrained to a non-negative integer followed by the 'x' character followed by another non-negative integer.

     <xsd:simpleType name="Boolean">
       <xsd:restriction base="xsd:anySimpleType">
         <xsd:enumeration value="Yes"/>
         <xsd:enumeration value="No"/>
       </xsd:restriction>
     </xsd:simpleType>

     <xsd:simpleType name="Dimension">
       <xsd:restriction base="xsd:anySimpleType">
         <xsd:pattern value="[0-9]+x[0-9]+"/>
       </xsd:restriction>
     </xsd:simpleType>

The following RDF/XML fragment shows the usage of these two datatypes (assuming the entity reference prf-dt is appropriately defined):

     <ex:SoundEnabled rdf:datatype="&prf-dt;#Boolean">Yes</ex:SoundEnabled>

     <ex:ScreenSize rdf:datatype="&prf-dt;#Dimension">160x160</ex:ScreenSize>

Although it is not a requirement, it is recommended that datatypes created in XML Schema (or any other language) to have rdfs:Datatype statements in the relevant RDF schema. The following RDF schema fragment contains such definitions for the Boolean and Dimension datatypes created above:

     <rdfs:Datatype rdf:about="&prf-dt;Boolean">
       <rdfs:comment xml:lang="en">...</rdfs:comment>
       <rdfs:subClassOf rdf:resource="&xsd;boolean"/>
     </rdfs:Datatype>

     <rdfs:Datatype rdf:about="&prf-dt;Dimension">
       <rdfs:comment xml:lang="en">...</rdfs:comment>
       <rdfs:subClassOf rdf:resource="&xsd;anySimpleType"/>
     </rdfs:Datatype>

Note that since rdfs:Datatype is a subclass of rdfs:Literal, a plain/untyped literal denotes an instance of rdfs:Literal and not an instance of rdfs:Datatype. The interpretation of untyped literals is fixed - they denote themselves.

If a property definition in a RDF schema contains a rdfs:range property whose rdf:resource attribute value is a datatype and an instance of this property (i.e. a property element in RDF/XML) does NOT contain an appropriate rdf:datatype attribute then the property element's value is considered an untyped literal. A property element MUST contain a rdf:datatype attribute for its value to be a typed literal. This requirement holds even if the property's definition in a schema contains a rdfs:range property whose value is a datatype.

### More Information

As of this writing the RDF Core Working Group has published several Working Drafts that contain information about datatyping in RDF. See the following documents and the sections listed for more information:

- RDF Primer
  - Typed Literals
  - Basic Principles
- RDF/XML Syntax Specification (Revised):
  - Typed Literals - rdf:datatype
- RDF Vocabulary Description Language 1.0: RDF Schema
- RDF Concepts and Abstract Syntax:
  - Datatypes
  - Literals
- RDF Semantics:
  - Datatype interpretations

Last Modified: 2002-12-23
Author: Art Barstow