STXT - Semantic Text
Built for humans. Reliable for machines.

STXT Templates (@stxt.template)

1. Introduction
2. Terminology
3. Relationship between STXT and Template
4. General structure of a Template
5. One template per target namespace
6. `Structure >>` block
7. Cardinalities
8. Types
9. ENUM and value list
10. Namespaces within `Structure`
11. Rules of “defined nodes”
12. Compilation to Schema (semantic equivalence)
13. Template Errors
14. Conformance
15. Meta-template of the `@stxt.template` system itself
16. Normative examples
17. Appendix A — Grammar (informal)
18. End of Document

1. Introduction

This document defines the specification of the STXT Template language, a mechanism for describing semantic rules (structure, types, cardinalities, and allowed values) applicable to STXT documents.

A template:

Relationship with @stxt.schema:

2. Terminology

The keywords "MUST", "MUST NOT", "SHOULD", "SHOULD NOT", and "MAY" must be interpreted according to RFC 2119.

Terms such as node, indentation, namespace, inline, and >> block retain their meaning in STXT-SPEC.

Additional definitions:

3. Relationship between STXT and Template

Validation through templates occurs after STXT parsing:

  1. Parsing the STXT document into a hierarchical structure.
  2. Resolving the effective namespace of each node.
  3. Selecting the corresponding template for the target namespace.
  4. Applying template rules (structure, cardinality, types, values).

An implementation MAY apply rules during parsing, provided that this validation remains loosely coupled to the base parser.

4. General structure of a Template

A template document MUST have as its root node:

Template (@stxt.template): <target_namespace>

The template MUST contain exactly one Structure node in block form (>>).

Template (@stxt.template): com.example.docs
	Description: Example template
	Structure >>
		Document (com.example.docs):
			Title: (1)
			Body: (1) TEXT

Rules:

5. One template per target namespace

For each logical namespace:

6. `Structure >>` block

The Structure >> block defines a tree of node templates using indentation, where:

6.1 Recommended style convention

For readability, it is recommended:

6.2 Syntax of a `Structure` line

Each line of the Structure >> block has the form:

<NodeName> [<NamespaceOverride>] ":" [<RuleSpec>]

where:

Examples:

Title:
Title: (1)
Body: (1) TEXT
Color: (?) ENUM [red, green, blue]
Body Content: (?) @Body Content
Metadata (org.example.meta): (0,1)

Notes:

6.3 Parsing rules for `Structure >>`

A template parser MUST:

  1. Read the Structure >> block as a sequence of lines (already canonicalized by STXT core: right trim per line).

  2. Ignore empty lines.

  3. Calculate hierarchy by indentation (same principles as STXT: consecutive indentation, without jumps).

  4. For each line, parse:

    • Node name + optional namespace (ns) if present.
    • The mandatory : character.
    • The optional rule specification (RuleSpec).
  5. Resolve the effective namespace of each line.

  6. Resolve @Node Name references only against nodes already previously defined in the same template.

A template parser MUST fail if a line does not contain :.

6.4 Redefinition of nodes and nodes from other namespaces

Templates distinguish between:

Rules:

Example:

Template (@stxt.template): com.example.dokumentando.doc
	Structure >>
		Document (com.example.dokumentando.doc):
			Title: (1)
			Sections: (*)
				Section: (+) TEXT
			Description: (1) TEXT
			Document Type: (1)
			Metadata (org.example.meta): (?)
		Summary (com.example.dokumentando.doc):
			Title: (1) @Title
			Content: (1) TEXT
			Metadata (org.example.meta): (?)

7. Cardinalities

Cardinality applies per instance of the parent node, counting only direct children that match by:

Cardinality is expressed as an optional token in parentheses: ( ... ).

7.1 Allowed forms

Allowed forms:

Form Meaning
num Exactly num.
* Any number (0..∞).
+ One or more (1..∞).
? Zero or one (0..1).
num+ num or more (num..∞).
num- Up to num (0..num).
min,max Between min and max.

Rules:

7.2 Default cardinality

If cardinality is not specified, the default value is:

8. Types

Types in templates reuse the set of types from STXT Schema, with the same intent: validate the form of the value and, optionally, its content.

The type is specified as a word after the cardinality (if present).

Examples:

Date: (1) DATE
Body: (1) TEXT
Is Public: (1) BOOLEAN

General rules:

8.1 Default type

If the type is omitted, the default type is:

8.2 Compatibility with children

A conforming implementation MUST apply these rules:

8.3 Set of types

The recommended set of types is the same as STXT-SCHEMA-SPEC.

A template implementation:

9. ENUM and value list

If the type is ENUM, the template MUST declare a list of allowed values, with at least one value.

The value list is specified with brackets [...] after the type:

ENUM [value1, value2, value3]

Color: (1) ENUM [red, green, blue]

Rules:

10. Namespaces within `Structure`

Each line may include an explicit namespace for the template node:

Metadata (org.example.meta): (?)

Rules:

11. Rules of “defined nodes”

In templates, a node becomes defined by its appearance in Structure. Unlike Schema, there is no separate Node: section; the structure itself is the definition.

Rules:

12. Compilation to Schema (semantic equivalence)

A template can be compiled into an equivalent schema:

Cardinality translation rules:

13. Template Errors

A template is invalid if any of these conditions occurs:

  1. The Structure>> document is not a valid STXT document (invalid indentation, etc.)
  2. The document does not have root Template (@stxt.template): <target_namespace>.
  3. Structure >> is missing or Structure is not a >> block.
  4. A non-empty line within Structure does not contain :.
  5. Malformed cardinality or cardinality with invalid numbers.
  6. Unknown type (according to the set of types supported by the implementation).
  7. Use of [...] if the type is not ENUM.
  8. BLOCK-only type with children defined in Structure.
  9. Redefining a local node that has already previously appeared without using a @Node Name reference.
  10. Using a @Node Name reference that does not point to a previous local definition.
  11. Declaring both a @Node Name reference and an explicit type on the same line.
  12. Declaring duplicate ENUM values after trim normalization.
  13. Defining children, explicit type, or ENUM values in a cross-namespace node.

14. Conformance

A template implementation is conforming if:

15. Meta-template of the `@stxt.template` system itself

This section defines a recommended minimal template to validate documents in the @stxt.template namespace.

Template (@stxt.template): @stxt.template
	Structure >>
		Template (@stxt.template): (1)
			Description: (?) TEXT
			Structure: (1) BLOCK

Notes:

16. Normative examples

16.1 Simple template (one namespace)

Template (@stxt.template): com.example.docs
	Description: Simple template
	Structure >>
		Document (com.example.docs):
			Title: (1)
			Author: (1)
			Date: (1) DATE
			Body: (1) TEXT

16.2 Template with repetition and nested nodes

Template (@stxt.template): com.example.blog.post
	Structure >>
		Post (com.example.blog.post):
			Title: (1)
			Slug: (1)
			Published: (1) BOOLEAN
			Tags: (?)
				Tag: (+)
			Sections: (1)
				Section: (+)
					Heading: (1)
					Content: (1) TEXT

16.3 Template with ENUM

Template (@stxt.template): com.example.ui.theme
	Structure >>
		Theme (com.example.ui.theme):
			Name: (1)
			Mode: (1) ENUM [light, dark]
			Accent: (?) ENUM [blue, green, orange]

16.4 Template with local reuse

Template (@stxt.template): org.example.docs
	Structure >>
		Email (org.example.docs):
			Body Content: (1) TEXT
		Demo (org.example.docs):
			Body Content: (?) @Body Content

16.5 Cross-namespace (external references)

Template (@stxt.template): com.example.docs
	Structure >>
		Document (com.example.docs):
			Metadata (org.example.meta): (?)
			Content: (1) TEXT

In this case:

17. Appendix A — Grammar (informal)

TemplateDoc      = "Template" "(" "@stxt.template" ")" ":" NamespaceTarget { TemplateField }
TemplateField    = DescriptionField | StructureField
DescriptionField = "Description" ":" Text
StructureField   = "Structure" ">>" Newline { StructureLine }

StructureLine    = Indent NodeSpec Newline
NodeSpec         = NodeName [NsOverride] ":" [RuleSpec]
NsOverride       = "(" ["@"] Namespace ")"
RuleSpec         = [Card] [NodeRef | Type [EnumValues]]

Card             = "(" CardToken ")"
CardToken        = "*" | "+" | "?" | Num | Num "+" | Num "-" | Num "," Num
NodeRef          = "@" NodeName
Type             = IdentUpper
EnumValues       = "[" Value { "," Value } "]"

NodeName         = Text up to `(` or `:`, with trim and compaction of spaces
NamespaceTarget  = Namespace according to STXT-SPEC
Namespace        = Ident { "." Ident }
Ident            = [a-z0-9]+
IdentUpper       = [A-Z0-9_]+  ; recommended in uppercase (style)

18. End of Document