An alternative proposal for XML validation : Relax-NG

Jean-François Perrot

  1. Principle
  2. Examples
  3. Restrictions (after XMLS)
  4. Namespaces
  5. Java-based validation


Relax NG = REgular LAnguage for XML Next Generation, promoted by OASIS

DocBook v.5 is specified via Relax-NG.
However, Eclipse provides no support for RNG-based validation.
  1. Principle

    Grammars rather than type systems, but vastly more powerful than DTDs.

    Two equivalent syntaxes :

    Java implementation : http://code.google.com/p/jing-trang/
    Download jing & trang ! And try your hand at using them on the examples below...
    Example scripts :

  2. Examples


  3. Pattern expressions

    The heart od RNG's superiority over XMLS lies in the flexibility of patterns.
    A pattern in RNG is the value of a pattern expression, made up of

    Exaample : a pattern for a node in a binary tree
        node = element node {
            (node, node) | leaf { empty }
        }


  4. Restrictions (after XMLS)

  5. Namespaces

    (Compare with XMLS and NS)

  6. Java-based validation

    1. Package javax.xml.validation
      Although the javadoc seems to indicate that RNGgrammars are in the scope of javax validation,
      direct application of the doc leads to a dismal failure at runtime :

              SchemaFactory factory =
                      SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);
           // as opposed to XMLSchema : XMLConstants.W3C_XML_SCHEMA_NS_URI
      ...........
      compiles, but does not run :

      Error : No SchemaFactory that implements the schema language specified by: http://relaxng.org/ns/structure/1.0 could be loaded


      A workaround is needed to make validation work with RNG grammars:

      import com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory; // Jing
      .............

                System.setProperty(
                    SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI,
                    "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"
                ); // necessary precaution
                SchemaFactory factory =
                             CompactSyntaxSchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);
                ............   
              

       to be compiled (and executed) with .....jing-20091111/bin/jing.jar (to be downloaded from jing-trang) on the classPath.

      See class VerifRNC, to be used with grammars in compact syntax (.rnc).
      If you wish to apply in XML format (.rng), use "com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory".

    2. Remember that this class validates XML documents that are read directly from file, via a StreamSource.
      However, if you try to apply such a validator to a DOMSource, as we did in Average_1V, execution aborts
      with an IllegalArgumentException (null)
      which, according to the JavaDoc, means that the Source is an XML artifact that the implementation cannot validate.

      Unfortunately, the original Java implementation of RNG-based validation (jing-trang) does not seem to offer an API which could be substituted to javax.xml.validation.
      Please let me know if you find more light on this subject!