- This version:
- http://www.semanticplanet.com/2003/08/rdft/spec-20030922
- Latest version:
- http://www.semanticplanet.com/2003/08/rdft/spec
- Previous version:
- http://www.semanticplanet.com/2003/08/rdft/spec-20030916
- Author:
- Ian Davis <iand@internetalchemy.org>
- Status:
- Early draft; for community review and feedback; some revisions expected; not feature complete; one implementation to date.
Copyright © 2003 Ian Davis.
This work is licensed under a Creative Commons License. An RDF representation of the license terms is embedded in the source of this document.
Table of Contents
- Introduction
- Changes From Previous Version
- Processing Model
- NodePaths
- Stylesheets
- Functions
- Open Issues
Introduction
RDF Templates work in a similar fashion to XSLT. However, instead of acting upon an XML document, an RDF Template acts on an RDF graph.
Changes From Previous Version
- Arc Selection — it is now possible to write NodePaths that select arcs.
- Reorganisation and Clarification — the ordering of the sections has been adjusted to better introduce fundamental concepts. Also many sections have been revised to clarify and strengthen the descriptions of the processes.
- Data Type Conversions — new conversions from arc list and arc to strings.
- Copyright and Licence — this specification is now licenced under a creative commons attribution licence.
Processing Model
An RDF Template stylesheet consists of a set of template rules. Each template rule has two parts: a pattern which is matched against the a graph and a template which can be instantiated to form part of the result tree. When a template is instantiated, it is always instantiated with respect to a context node and a context graph. The context node is always a member of the context graph.
A template can contain elements that specify literal result element structure. A template can also contain elements from the RDFT namespace that are instructions for creating result tree fragments. When a template is instantiated, each instruction is evaluated and replaced by the result tree fragment that it creates. Instructions can select and process nodes and arcs in the source graph. Nodes can be selected for matching against further template rules.
Graphs
The input RDF document must be parsed into its graph form before the stylesheet can be
evaluated. This graph is called the source graph and consists of the triples extracted
from the input document. Before stylesheet evaluation starts the RDFT processor must apply the rules specified
in section 4.2 of the RDF Semantics specification
to obtain the the input graph. The
stylesheet author may use the import stylesheet element to import external
RDF documents into the source graph before processing of templates starts. These imported documents
will also be subject to the RDFS closure rules and form part of the input graph but not part of the source graph.
Each instruction in the stylesheet is evaluated against a context node or arc which is part of a context graph. The context graph is the graph from which the context node or arc was selected. This may be either the input graph or the source graph. Note that all nodes and arcs in the source graph also exist in the input graph. However, if the context node or arc was selected explictly from the source graph then further NodePath selections using that context node or arc will also operate over the source graph.
Data Model
The processing model uses the following data types:
- node — represents a node in a graph.
- node list — represents a list of nodes.
- arc — represents an arc in a graph.
- arc list — represents a list of arcs.
- string — an ordered sequence of characters.
The processor may convert between the various data types according to the folowing rules:
- node list to string — concatenate the string representations of each node in the list. The order in which they are concatenated is determined by the ordering of the node list which may be undefined.
- node to string — if the node is a literal node then the string representation is equal to the node's label. If the node is a resource then the string representation is the value of the node's
rdf:valueproperty. If nordf:valueproperty is present then the string representation is the empty string. - node to node list — construct an empty node list and append the node to the list.
- string to node — construct a literal node with the string as its value. The literal node language is undefined.
- arc to string — the string representation of the node that is the value of the arc.
- arc list to string — concatenate the string representations of each arc in the list. The order in which they are concatenated is determined by the ordering of the arc list which may be undefined.
- arc to arc list — construct an empty arc list and append the arc to the list.
All other data type conversions are forbidden.
Template Priorities
When more than one template rule has a pattern that matches the context node then the most specific rule will be selected. The most specific rule is deemed to be the one that performs the most comparisons. In the event of two matching rules having identical degrees of specificity then the rule associated with the last template in document order will be selected.
Root Templates
Each RDFT stylesheet may have one root template. This template will
be the first template to be instantiated by the RDFT processor. The root template
cannot have a nodePattern attribute. The root template
will be instantiated with no context. Therefore any selection nodepaths
should be scoped globally or at source level since there is no context node or arc to be
matched.
NodePaths
NodePaths are strings that specify criteria that are used by the RDFT processor to build a list of matching nodes or arcs from the context graph.
The NodePath is evaluated against the context graph and any matching nodes or arcs are identified. Each matching node or arc is added to a list of matches. A NodePath may only match nodes or arcs, but not both.
NodePaths consist of a series of alternating NodePattern and ArcPatterns. The first pattern in a NodePath must correspond to the context in which the NodePath is evaluated, i.e.for a NodePath in a node context the first pattern must be a NodePattern and in an arc context the pattern must be an ArcPattern. The last pattern in a NodePath determines the selection type of the NodePath. A terminating NodePattern indicates that the NodePath is node-selecting; a terminating ArcPattern denotes an arc-selecting NodePath.
A NodePath is evaluated by matching the first pattern against the context node or arc as appropriate. If the pattern matches the context node or arc then the next pattern is applied to either all the arcs of the matching context node or the value node of the matching context arc. The list of arcs or nodes that match a pattern is used as the context for the following pattern.
For example, given the following NodePath:
node-pattern-1/arc-pattern-1/node-pattern-2/arc-pattern-2/node-pattern-3
the RDFT processor will evaluate node-pattern-1 against the context node. If
the context node matches the pattern then the processor will produce a node list consisting
of the context node only. The RDFT processor will iterate through this list and evaluate arc-pattern-1
for every arc of the context node. Each matching arc will be added to a new arc list. The RDFT processor
will then iterate through this arc list and evaluate node-pattern-2 to the value node of each arc in the list.
Matching nodes will be added to a new node list. The RDFT processor then iterates through this node list
and evaluates arc-pattern-2 for every arc of every node in the node list. All matching arcs are added to
a new arc list. Finally the RDFT processor iterates through this arc list and evaluates node-pattern-3
against each value node of each arc. Matching nodes are added to a new node list. This node list becomes the
final result of the NodePath.
NodePath Terminology
A NodePath is evaluated in a particular context which is determined by the structure of the stylesheet and may be either node-context or arc-context.
A node-matching NodePath has a NodePattern as its first pattern and is evaluated in a node-context. It is an error for this type of NodePath to be evaluated when the context is an arc.
An arc-matching NodePath has an ArcPattern as its first pattern and is evaluated in an arc-context. It is an error for this type of NodePath to be evaluated when the context is an arc.
NodePaths can change the context in which they are evaluated by the use of scope specifiers. A scope specifier overrides the current context by selecting a list of nodes or arcs from a graph. The remainder of the NodePath is evaluated against each node or arc in the list as though it were the context node or arc.
A global scope specifier selects all nodes from the input graph. This scope specifier can only be used with a node-matching NodePath.
A source scope specifier selects all nodes from the source graph. This scope specifier can only be used with a node-matching NodePath.
NodePaths can select either nodes or arcs.
A node-selecting NodePath has a NodePattern as a terminating pattern and selects a list of nodes from the graph.
An arc-selecting NodePath has an ArcPattern as a terminating pattern and selects arcs from the graph.
NodePath Syntax
A NodePath may start with an optional ScopeSpecifier which determines the context in which the NodePath is evaluated. If no ScopeSpecifier is supplied then the NodePath will be evaluated against the current context node or arc.
The remainder of the NodePath consists of an alternating sequence of NodePatterns and ArcPatterns separated by forward slashes '/'.
A NodePattern has the following components:
- A mandatory NodeSpecifier which determines the base type of node that this pattern will match. This may be one of:
- node() — match any type of node, i.e. resource or literal. An asterisk ( * ) may be used as a synonym for
node(). - resource() — match any resource node.
- resource('uriref') — match the resource node with the specified uriref.
- literal() — match any literal node.
- literal('value') — match any literal node with the specified label.
- literal('value', 'lang') — match any literal node with the specified label and language code as specified in Section 6.5 of the RDF Concepts document.
- literal(*, 'lang') — match any literal node with the a language code as specified in Section 6.5 of the RDF Concepts document.
- subject() — match any resource node that is the subject of a triple in the context graph.
- node() — match any type of node, i.e. resource or literal. An asterisk ( * ) may be used as a synonym for
- Zero or more ResourceCondition patterns which specify conditions to be applied to any node that matches the NodeSpecifier. Each ResourceCondition is enclosed by square brackets ( '[' and ']' ). Multiple ResourceConditions are applied in the sequence specified. The node being tested must pass all ResourceConditions to pass the entire NodePattern, i.e. the ResourceConditions are 'ANDed' together. This specification does not define a method of performing 'OR' tests for NodePatterns.
An ArcPattern has the following components:
- A mandatory NodeSpecifier which determines the base type of node that this pattern will match. This may be one of:
- node() — match any type of node, i.e. resource or literal. An asterisk ( '*' ) may be used as a synonym for
node(). - resource() — match any resource node.
- resource('uriref') — match the resource node with the specified uriref.
- subject() — match any resource node that is the subject of a triple in the context graph.
- node() — match any type of node, i.e. resource or literal. An asterisk ( '*' ) may be used as a synonym for
- Zero or more ResourceCondition patterns which specify conditions to be applied to any node that matches the NodeSpecifier. Each ResourceCondition is enclosed by square brackets ( '[' and ']' ). Multiple ResourceConditions are applied in the sequence specified. The node being tested must pass all ResourceConditions to pass the entire NodePattern, i.e. the ResourceConditions are 'ANDed' together. This specification does not define a method of performing 'OR' tests for NodePatterns.
NodePath BNF
Note the single quotes in the UriResourceSpecifier and LabelLiteralSpecifier productions, i.e. you should write resource('http://example.com/')
NodePath ::= [ ScopeSpecifier ] NodeMatchingNodePath | ArcMatchingNodePath
ScopeSpecifier ::= GlobalScopeSpecifier | SourceScopeSpecifier
GlobalScopeSpecifier ::= "/"
SourceScopeSpecifier ::= "~"
NodeMatchingNodePath ::= NodePattern [ "/" ArcMatchingNodePath ]
ArcMatchingNodePath ::= ArcPattern [ "/" NodeMatchingNodePath ]
NodePattern ::= NodeSpecifier { "[" ResourceCondition "]" }
ArcPattern ::= NodeOrResourceSpecifier { "[" ResourceCondition "]" }
NodeSpecifier ::= NodeOrResourceSpecifier | LiteralSpecifier
NodeOrResourceSpecifier ::= AnyNodeSpecifier | ResourceSpecifier
AnyNodeSpecifier ::= "node()" | "*"
ResourceSpecifier ::= AnyResourceSpecifier | UriResourceSpecifier | SubjectSpecifier
AnyResourceSpecifier ::= "resource()"
UriResourceSpecifier ::= "resource('" UriReference "')"
LiteralSpecifier ::= AnyLabelLiteralSpecifier | LabelLiteralSpecifier |
LabelLangLiteralSpecifier | AnyLabelLangLiteralSpecifier
AnyLabelLiteralSpecifier ::= "literal()"
LabelLiteralSpecifier ::= "literal('" LiteralValue "')" | "literal('" LiteralValue "', *)"
LabelLangLiteralSpecifier ::= "literal('" LiteralValue "','" LiteralLanguage "')"
AnyLabelLangLiteralSpecifier ::= "literal(*,'" LiteralLanguage "')"
SubjectSpecifier ::= "subject()"
ResourceCondition ::= NodeSelector [ " = " NodeSpecifier ]
NodeSelector ::= ArcPattern "/" NodePattern
UriReference ::= A string-value interpreted as an RDF URI Reference
LiteralValue ::= Any XML element content that is allowed
according to XML definition Content of Elements Rule [43] content
NodePath Examples
/node()- A node-matching, node-selecting, globally-scoped NodePath that selects all nodes in the context graph.
/resource()- A node-matching, node-selecting, globally-scoped NodePath that selects all resource nodes in the context graph.
/literal()- A node-matching, node-selecting, globally-scoped NodePath that selects all literal nodes in the context graph.
resource('http://example.com/')- A NodePath that selects the context node or arc only if it is a resource with the given uriref. This NodePath may be either node-selecting or arc-selecting depending on the context.
resource()[resource('http://ex.example.com/age')/literal() = literal('23')]- Selects the context node or arc only if it has a 'http://ex.example.com/age' property with a literal value of '23'. This NodePath may be either node-selecting or arc-selecting depending on the context.
/resource()[resource('http://ex.example.com/age')/literal() = literal('23')]- A globally scoped NodePath that selects all resource nodes from the RDFS closed graph that have a 'http://ex.example.com/age' property with a literal value of '23'.
~resource()[resource('http://ex.example.com/age')/literal() = literal('23')]- A source scoped NodePath that selects all resource nodes from the original source graph that have a 'http://ex.example.com/age' property with a literal value of '23'.
resource()/resource('http://ex.example.com/name')/literal()- A locally scoped NodePath that selects all literal nodes that are the value of any 'http://ex.example.com/name' property of the context node.
resource()/resource()/node()- A locally scoped NodePath that selects all nodes that are the value of any property of the context node.
*/resource()/*- A locally scoped NodePath that selects all nodes that are the value of any property of the context node.
*/resource()/*[resource('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')/resource() = resource('http://xmlns.com/foaf/0.1/Person')]- A locally scoped NodePath that selects all nodes that have an RDF type of http://xmlns.com/foaf/0.1/Person and are the value of any property of the context node.
*/*[resource('http://www.w3.org/2000/01/rdf-schema#subPropertyOf')/resource() = resource('http://xmlns.com/foaf/0.1/knows')]/*- A locally scoped NodePath that selects all nodes that are the value of any property of the context node that is an RDFS sub property of http://xmlns.com/foaf/0.1/knows.
/subject()- A globally scoped NodePath that selects all nodes in the context graph that are the subject of any triple in that graph.
resource('http://example.com/')- A locally scoped NodePath that selects the context node only if it has the given uriref and is the subject of any triple in the graph.
NodePattern Examples
resource()- A NodePattern that matches the context node only if it is a resource.
resource()- A NodePattern that matches the context node only if it is a literal.
resource('http://example.com/')- A NodePattern that matches the context node only if it is a resource with uriref
http://example.com/. literal('dingo')- A NodePattern that matches the context node only if it is a literal with label
dingo. resource()[resource('http://ex.example.com/age')/literal() = literal('23')]- A NodePattern that matches the context node only if it has a
http://ex.example.com/ageproperty with a literal value of23.
Stylesheets
Namespace URI
The RDFT namespace has the URI http://purl.org/vocab/2003/rdft/
Stylesheet Elements
stylesheet
Attributes:
- version
- a text literal
<rt:stylesheet rt:version=number xmlns:rt="http://purl.org/vocab/2003/rdft/">
<!-- Content: top-level-elements -->
</rdft:stylesheet>
An stylesheet element must have a
version attribute, indicating the version of RDFT that
the stylesheet requires. For this version of RDFT, the value should
be 1.0.
The stylesheet element may contain the following elements from the RDFT namespace plus
any elements from other namespaces:
- macro-set
- root-template
- template
template
Attributes:
- pattern (required)
- a NodePattern
- name
- a text literal
The template element specifies a template rule. It has a single required attribute pattern which
specifies the NodePattern that will cause this rule to be applied. When the rule
is fired the parse tree of contained elements is traversed. Each element from a namespace
other than the RDFT namespace is copied into the result tree. Any elements in the
RDFT namespace trigger further processing as described in this document.
The template element may contain the following elements from the RDFT namespace plus
any elements from other namespaces:
- value-of
- apply-templates
- element
- attribute
- for-each
root-template
Attributes:
- none
The root-template element specifies the initial template rule that will be used by the RDFT processor when it first starts processing the input graph. This template will always be the first template instantiated by the RDFT processor.
If no root-template element is specified the RDFT processor will supply the following default template:
<rt:root-template>
<rt:apply-templates rt:select="/node()"/>
</rt:root-template>
This element may contain any elements allowed for the template element.
import
Attributes:
- src (required)
- the URI of some RDF to import
The import element specifies the URI of an RDF document that
should be merged with the source graph before stylesheet processing is to begin.
The import element may not contain any other elements.
macro-set
Attributes:
- prefix (required)
- a text literal corresponding to the NCName production
The macro-set element defines a group of NodePath replacement macros. The prefix attribute
is required and is used to provide a naming scope for the contained macro definitions.
The macro-set element may contain the following elements from the RDFT namespace plus
any elements from other namespaces:
- macro
macro
Attributes:
- name (required)
- a text literal corresponding to the NCName production
- value (required)
- a text literal
The macro element defines a NodePath replacement macro. The macro may be
used in place of any named syntax component in the NodePath BNF. The macro may be
referred to in a NodePath using the following syntax:
prefix:name
The RDFT processor will perform a substitution of the specified value in place of the macro name when parsing a NodePath.
Note that the macro reference is not a QName.
The macro element may not contain any other elements.
Given the following macro definition:
<rt:macro-set rt:prefix="rdf">
<rt:macro name="type" value="resource('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')/resource()"/>
</rt:macro-set>
The following NodePaths are equivalent:
node()[rdf:type = resource('http://foo.example.com')]
node()[resource('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')/resource() = resource('http://foo.example.com')]
As are the following:
node()/rdf:type
node()/resource('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')/resource()
value-of
Attributes:
This element evaluates the expression in the select attribute and returns the result converted to a string according to the conversion rules in the Data Model section of this document.
The value-of element may not contain any elements from any namespace.
apply-templates
Attributes:
- select (required)
- a node-selecting NodePath
This element uses the NodePath specified in the
select attribute to build a list of matching nodes. The RDFT processor
examines each node in turn and determines the best matching template rule in the stylesheet.
The template rule is then instantiated with the node as the context node.
The order in which the nodes are selected is undefined in this version of the specification.
The apply-templates element may not contain any elements from any namespace.
for-each
Attributes:
- select (required)
- a node-selecting or arc-selecting NodePath
This element uses the NodePath specified in the
select attribute to build a list of matching nodes or arcs. The RDFT processor
examines each node or arc in turn and evaluates the contents of this element using the node or arc as
the context node or arc.
The order in which the nodes or arcs are selected is undefined in this version of the specification.
This element may contain any elements allowed for the template element.
element
Attributes:
- name (required)
- a literal text value
This element outputs an XML element with the specified name to the result tree.
This element may contain any elements allowed for the template element.
attribute
Attributes:
- name (required)
- a literal text value
- value
- a literal text value
This element outputs an XML attribute with the specified name and value to the result tree.
This element may contain any elements allowed for the template element.
Functions
Functions can be used by the rt:value-of element to access additional
information about the nodes selected.
label()Open Issues
Lots of issues, of which these are only a taster...
- graph infinite loops handling
- ContextNode selector - need some way of referring to the context node
- Analogs of other XSLT elements: options, if, choose, when, variable, param, named templates
- Namespace handling for output of element and attributes
- Literal datatyping
- Incorporate rdf:Seq to determine sequence of processing for-each etc