Chapter 2. Loading grammars
Before it can be used, a parser must be constructed for the grammar. You can load Invisible XML grammars in either the text or XML syntax. You can also load grammars in the CoffeeGrinder compiled format.
The API has been redesigned and greatly simplified in version 2.x. Instead of having a few different functions that take a grammar as an argument, there are now just a couple of functions that return a parser for the grammar.
The parser creation functions return an XPath function that takes a single argument, the string to be parsed. If the grammar cannot be parsed, an error is raised. This error can be captured with try/catch.
2.1. cs:load-grammar
This function takes the URI of an Invisible XML grammar and constructs a parser for that grammar.
$uri | xs:string | A URI that identifies an Invisible XML grammar |
$uri | xs:string | A URI that identifies an Invisible XML grammar |
$options | map(xs:string, item()) | Grammar options |
See Chapter 5, Parser options for a description of the available options.
If the return format
is XML, an XML node will be returned.
For the JSON return types, an XDM map will be returned.
2.2. cs:make-parser
This function takes an Invisible XML grammar and constructs a parser for that grammar.
$input | item() | An input grammar |
$input | item() | An input grammar |
$options | map(xs:string, item()) | Grammar options |
Exactly what this function does depends on the type of
$input
.
- XML node
-
The input is assumed to be an XML version of an invisible XML grammar (a “visible XML grammar”). The function will attempt to construct a parser for that grammar.
xs:anyURI
-
The input is assumed to be a URI that identifies the location of an invisible XML grammar. It will be loaded and parsed just as
cs:load-grammar
would. xs:anyAtomicType
-
The string value of the input is assumed to be an Invisible XML grammar. The function will attempt to construct a parser for that grammar.
- anything else
-
Any other input will cause an error.
See Chapter 5, Parser options for a description of the available options.
If the return format
is XML, an XML node will be returned.
For the JSON return types, an XDM map will be returned.