Main Page » XQuery » XQuery 4.0 » Invisible XML

Invisible XML

BaseX supports the functionality of Invisible XML by making use of Gunther Rademacher’s Markup Blitz implementation. With Invisible XML, text content can be parsed according to a context-free grammar and transformed into XML.

Prerequisites

Invisible XML will be available if the Markup Blitz library is found in the classpath. This will be the case if you use the full distributions of BaseX. Alternatively, you can download the library from Maven Central. With BaseX 11.0, use version 1.4 as available in markup-blitz-1.4.jar.

Conventions

Namespace prefix ixml, which is used by attributes of Invisible XML result elements, refers to namespace URI http://invisiblexml.org/NS.

Functions

fn:invisible-xml

Signature
fn:invisible-xml(
  $grammar  as item()?  := (),
  $options  as map(*)?  := {}
) as fn(xs:string) as item()
Summary

The function accepts an Invisible XML $grammar, which describes some input language, and returns another function that implements a parser for that language.

If $grammar is an empty sequence, a parser for the Invisible XML specification grammar is returned.

The following $options are available:

optiondefaultdescription
fail‑on‑errorfalse() Raise an error if the parsing function fails.

By default, when parsing errors occur, the parsing function returns an element with the attribute ixml:state, whose value includes the word failed. However, if fail‑on‑error is set to true(), a parsing error will cause the parsing function to fail with the error code err:FOIX0001.

Result Function

The function that is returned by fn:invisible-xml transforms its input string into an XML structure as specified by the grammar. It returns a document node. The document element may contain an ixml:state attribute, which can include one or more of the following values:

Value Description
failed A parsing error has occurred. In this case, the element content provides more details.
ambiguous The input is ambiguous with respect to the grammar. The resulting document represents one valid parse, but others exist.
version‑mismatch The grammar prolog requested a specific version of Invisible XML that is not supported.

If neither of the above applies, the ixml:state attribute will be absent.

Example

The following grammar describes a simple arithmetic expression.

Grammar:
-expression : term;
              add;
              subtract.
        add : expression, -'+', term.
   subtract : expression, -'-', term.
      -term : factor;
              multiply;
              divide.
   multiply : term, -'*', factor.      
     divide : term, -'/', factor.      
    -factor : integer;
              negate;
              -'+', factor;
              -'(', expression, -')'.
     negate : -'-', factor.
    integer : @value.
      value : (-'+'; '-')?, ['0'-'9']+.

The result is a structured representation of the expression that preserves the precedence of arithmetic operators.

Query:
invisible-xml($grammar)("3*13+3")
Result:
<add>
  <multiply>
    <integer value="3"/>
    <integer value="13"/>
  </multiply>
  <integer value="3"/>
</add>

The grammar is ambiguous, because it allows plus and minus signs in either of the factor or value rules. A double minus sign, preceding an value, thus can be interpreted as two negations of a positive integer, or one negation of a negative integer. The parser will return one of these interpretations and indicate the result as ambiguous in the ixml:state attribute.

Query:
invisible-xml($grammar)("--1")
Result:
<negate xmlns:ixml="http://invisiblexml.org/NS" ixml:state="ambiguous">
  <integer value="-1"/>
</negate>

Changelog

Version 11.0
  • Added: Full support for Invisible XML, utilizing Gunther Rademacher’s Markup Blitz implementation.

⚡Generated with XQuery