- This version:
- http://www.semanticplanet.com/2003/08/rdft/spec-20030909
- Latest version:
- http://www.semanticplanet.com/2003/08/rdft/spec
- Previous version:
- http://www.semanticplanet.com/2003/08/rdft/spec-20030904
- Author:
- Ian Davis <iand@internetalchemy.org>
- Status:
- Early draft; for community review and feedback; some revisions expected; not feature complete; one implementation to date
Table of Contents
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
The following changes were due to suggestions by Daniel Biddle:
- Specify RFC 3066 codes
- Literal(*, 'en-GB-oed') instead of literal(, 'en-GB-oed')
- Minor typographical changes to spec. text.
Processing Model
A transformation expressed in RDFT describes rules for creating a result tree from an RDF graph. This is achieved by associating patterns with templates. A pattern is matched against nodes and arcs in the source graph.
An RDF Template stylesheet consists of a set of template rules. Each template rule has two parts: a pattern which is matched against the source graph and a template which can be instantiated to form part of the result tree.
A template is instantiated for a particular context node to create part of the result tree. 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 executed and replaced by the result tree fragment that it creates. Instructions can select and process nodes of the source graph. Processing a node creates a result tree fragment by finding the applicable template rule and instantiating its template.
Note that nodes are only processed when they have been selected by the execution of an instruction. The result tree is constructed by finding the template rule for the context node and instantiating its template.
In the process of finding the applicable template rule, more than one template rule may have a pattern that matches a given context node. However, only one template rule will be applied. The method for deciding which template rule to apply is described later in this document.
When a template is instantiated, it is always instantiated with respect to a current node and a context graph. The current node is always a member of the context graph.
Template Priorities
When more than one template rule has a pattern that matches the context node then the most specific rule (the one that performs the most comparisons) will be selected. 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 node. Therefore any selection nodepaths
should be scoped globally.
Source Graph
The stylesheet is applied to a source graph which is usually created from an input
RDF document. The RDFT processor must apply the rules specified in section 4.2 of the RDF Semantics specification
to obtain the the rdfs-closure of the source graph before the stylesheet is applied. The
stylesheet author may use the import stylesheet element to import external
RDF documents into the source graph before processing of templates starts.
NodePaths
NodePaths are strings that specify criteria that are used by the RDFT processor to build a list of matching nodes from the context graph.
The NodePath is evaluated against the context graph and any matching nodes are identified. Each matching node is added to a list of matches.
A NodePath may either have local scope in which the NodePath is applied only to the context node or global scope in which the NodePath is applied to every node in the context graph in turn.
NodePath Syntax
A NodePath consists of a number of components:
- An optional ScopeSpecifier which determines whether the NodePath operates globally or locally.
- A mandatory NodePattern which specifies criteria which must be passed by the context node for the NodePath to continue.
- Zero or more NodeSelector patterns which specify arc/node traversals from the context node.
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 fornode().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.
- 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 ] NodePattern { "/" NodeSelector }
ScopeSpecifier ::= "/"
NodePattern ::= NodeSpecifier { "[" ResourceCondition "]" }
ResourceCondition ::= NodeSelector [ " = " NodeSpecifier ]
NodeSpecifier ::= AnyNodeSpecifier | ResourceSpecifier | LiteralSpecifier
AnyNodeSpecifier ::= "node()" | "*"
ResourceSpecifier ::= AnyResourceSpecifier | UriResourceSpecifier
AnyResourceSpecifier ::= "resource()"
UriResourceSpecifier ::= "resource('" UriReference "')"
UriReference ::= A string-value interpreted as an RDF URI Reference
LiteralSpecifier ::= AnyLabelLiteralSpecifier | LabelLiteralSpecifier |
LabelLangLiteralSpecifier | AnyLabelLangLiteralSpecifier
AnyLabelLiteralSpecifier ::= "literal()"
LabelLiteralSpecifier ::= "literal('" LiteralValue "')" | "literal('" LiteralValue "', *)"
LabelLangLiteralSpecifier ::= "literal('" LiteralValue "','" LiteralLanguage "')"
AnyLabelLangLiteralSpecifier ::= "literal(*,'"LiteralLanguage "')"
LiteralValue ::= Any XML element content that is allowed
according to XML definition Content of Elements Rule [43] content
NodeSelector ::= ArcPattern "/" NodePattern
ArcPattern ::= NodeOrResourceSpecifier { "[" ResourceCondition "]" }
NodeOrResourceSpecifier ::= AnyNodeSpecifier | CurrentNodeSpecifier | ResourceSpecifier
NodePath Examples
/node()- A globally scoped NodePath that selects all nodes in the context graph.
/resource()- A globally scoped NodePath that selects all resource nodes in the context graph.
/literal()- A globally scoped NodePath that selects all literal nodes in the context graph.
resource('http://example.com/')- A locally scoped NodePath that selects the context node only if it has the given uriref.
resource()[resource('http://ex.example.com/age')/literal() = literal('23')]- A locally scoped NodePath that selects the context node only if it has 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.
*/*/*- A locally scoped NodePath that selects all nodes that are the value of any property of the context node.
*/*/*[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.
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:
- select (required)
- a NodePath
This element is replaced by the literal value of the concatenated values of
the nodes selected by the NodePath specified in
the select attribute. Each literal node specified is replaced by its
label. Each resource node is replaced by its rdf:value property
if present or an empty string if not.
The order in which the nodes are selected is undefined in this version of the specification.
The value-of element may not contain any elements from any namespace.
apply-templates
Attributes:
- select (required)
- a 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 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 applies the contents of this element to the node, adding
the output for each node to the result tree.
The order in which the nodes 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.
Open Issues
Lots of issues, of which these are only a taster...
- graph infinite loops handling
- label() function to return the label of the current node
- ContextNode selector - need some way of referring to the context node
- Analogs of other XSLT elements: options, if, choose, when, variable, param, named templates
- Literal datatyping
- Incorporate rdf:Seq to determine sequence of processing for-each etc