Skip to content

Instantly share code, notes, and snippets.

@miikka
Created January 12, 2016 19:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miikka/b137a8705e88b0b6c164 to your computer and use it in GitHub Desktop.
Save miikka/b137a8705e88b0b6c164 to your computer and use it in GitHub Desktop.
RELAX NG compact syntax cheat sheet
# RELAX NG compact syntax cheat sheet
# See also:
# * tutorial <http://www.relaxng.org/compact-tutorial-20030326.html>
# * spec <http://www.relaxng.org/compact-20021121.html>
# Checking a XML file:
#
# jing -c schema.rnc file.xml
#
# Converting the compact syntax to the regular (XML) syntax:
#
# trang -i rnc -o rng schema.rnc schema.rng
# For a simple schema, the top-level can contain the root element definition
element root {
# mandatory attribute
attribute foo { text },
# optional attribute
attribute bar { text }?,
# nested elements
element nested { element even_more_nested { empty } },
# empty elements (<empty/>)
element empty { empty },
# elements containing text (<tag>whatever</tag>)
element tag { text },
# choice (either <a/> or <b/>)
(element a { empty } | element b { empty }),
# if the order of the elements does not matter, use "&" instead of ","
# (this matches <a/><b/> as well as <b/><a/>)
element container {
element a { empty } &
element b { empty }
},
# text mixed with elements (foo<a/>bar)
element container2 {
# this is the same as "text & element a { empty }"
mixed { element a { empty } }
},
# quantifiers:
# ?: zero or one occurence (item is optional)
# *: zero or more occurences
# +: one or more occurences
element q1 { text }?,
element q2 { text }*,
element q3 { text }+
# * matches any name
element wildcards {
attribute * { text },
element * { empty }
}
}
# For complex schemata, use grammar { }
grammar {
# start defines the root element
start = element root {
# elements can refer to other definitions
recursive-a*
}
# the definitions can be recursive (e.g. <a><a><a/></a></a>)
recursive-a = (element a { recursive-a } | empty)
}
# Not covered by this cheatsheet: datatypes, lists, annotations, namespaces,
# external patterns, nameclass connectors, etc. See the spec and the tutorial!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment