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. “Defined nodes” rules
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 to describe semantic rules (structure, types, and cardinalities) applicable to STXT documents.

A template:

Relationship with @stxt.schema:

2. Terminology

The key words "MUST", "MUST NOT", "SHOULD", "SHOULD NOT", and "MAY" are to be interpreted as described in 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 using templates occurs after STXT parsing:

  1. Parsing the STXT document into a hierarchical structure.
  2. Resolving the logical namespace by inheritance.
  3. Selecting the template corresponding to the logical namespace.
  4. Applying template rules (structure, cardinality, types, values).

Optionally, a validator MAY apply rules during parsing, as long as it is loosely coupled to the parser.

4. General structure of a Template

A template is a document whose root node is:

Template (@stxt.template): <target_namespace>

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

Template (@stxt.template): com.example.docs
	Description: Example template
	Structure >>
		Document:
			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]
Metadata (com.google.html): (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. Compute hierarchy by indentation (same principles as STXT: consecutive indentation, no jumps).
  4. For each line, parse:
    • Node name + optional namespace (ns) if present.
    • The mandatory : character.
    • The optional rule specification (RuleSpec).

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

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 no cardinality is specified, the default value is:

8. Types

Types in templates reuse the set of STXT Schema types, with the same intent: validate the shape 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

8.1 Default type

If the type is omitted, the default type is:

8.2 Compatibility with children

A conforming validator MUST apply these rules:

8.3 Type set

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

A template validator:

9. ENUM and value list

If the type is ENUM, the template MAY declare a list of allowed values.

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 node template:

Metadata (com.google.html): (?)

Rules:

Motivation:

11. “Defined nodes” rules

In templates, a node exists if it appears in Structure. Unlike Schema, there is no separate Node: section; the structure itself is the definition.

Recommended consistency rule:

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 occur:

  1. The document does not have root Template (@stxt.template): <target_namespace>.
  2. Structure >> is missing or Structure is not a >> block.
  3. A non-empty line within Structure does not contain :.
  4. Malformed cardinality or invalid numbers.
  5. Unknown type (according to the set of types supported by the validator).
  6. Use of [...] if the type is not ENUM.
  7. BLOCK-only type with children defined in Structure.
  8. Invalid indentation within Structure (level jumps, etc.).

14. Conformance

A template implementation is conforming if it:

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

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

Template (@stxt.template): @stxt.template
	Structure >>
		Template: (1)
			Description: (0,1) 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:
			Title: (1)
			Author: (1)
			Date: (1) DATE
			Body: (1) TEXT

16.2 Template with repetition and nested nodes

Template (@stxt.template): com.blog.post
	Structure >>
		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.ui.theme
	Structure >>
		Theme:
			Name: (1)
			Mode: (1) ENUM [light, dark]
			Accent: (?) ENUM [blue, green, orange]

16.4 Cross-namespace (external references)

Template (@stxt.template): com.example.docs
	Structure >>
		Document:
			Metadata (com.google.html): (?)
			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] [Type] [EnumValues]

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

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

18. End of Document