Search code examples
xmldocbookschematronrelaxng

What is the relationship between RELAX NG and Schematron wrt DocBook?


I'm quite sure that the question will need refinement, but wasn't able to better articulate my confusion in one sentence:

DocBook 5.2: The Definitive Guide, section 1.1. A Short DocBook History states that

Starting with DocBook V5.0, DocBook is exclusively an XML vocabulary defined with RELAX NG and Schematron.

According to Wikipedia on RELAX NG, it is a "schema language for XML—a RELAX NG schema specifies a pattern for the structure and content of an XML document".

Schematron on the other hand "is a rule-based validation language for making assertions about the presence or absence of patterns in XML trees. It is a structural schema language expressed in XML."

So both are schema languages, but

  • RELAX NG is used to define a vocabulary (which is kind of a domain-specific language expressed with tags(?); a semantic markup language in the case of DocBook) for creating XML documents

  • Schematron is used to validate XML documents based on their associated XML schemas (using jing for example, I guess?)

I assumed based on the DocBook wikipedia line

[DocBook v5.x] is formally defined by a RELAX NG schema with integrated Schematron rules

that there is a relationship between them. Also, does this imply that a RELAX NG XML schema is not flexible enough to contain all the rules to use it to validate a document?


Probably missing something fundamental: found the question RelaxNG vs XML schema, but I truly thought that one creates XML schemas with RELAX NG, so the question doesn't makes sense to me, even after reading the answers...


Related


Solution

  • These are two small extracts from the compact-syntax RELAX NG schema at http://www.oasis-open.org/docbook/xml/5.0b5/rng/docbook.rnc:

        element segmentedlist {
          db.segmentedlist.attlist,
          db.segmentedlist.info,
          db.segtitle+,
          db.seglistitem+
        }
    
      db.seglistitem =
        
        ## A list item in a segmented list
        [
          s:pattern [
            name = "Cardinality of segments and titles"
            "\x{a}" ~
            "          "
            s:rule [
              context = "db:seglistitem"
              "\x{a}" ~
              "            "
              s:assert [
                test = "count(db:seg) = count(../db:segtitle)"
                "The number of seg elements must be the same as the number of segtitle elements in the parent segmentedlist"
              ]
              "\x{a}" ~
              "          "
            ]
            "\x{a}" ~
            "        "
          ]
        ]
        element seglistitem { db.seglistitem.attlist, db.seg+ }
    

    The db.seglistitem RNG pattern has some embedded Schematron between the outermost [ and ].

    The db.seglistitem RNG pattern defines the structure of the seglistitem element. RNG is better at defining co-constraints than any other XML schema language of which I am aware, but it can't do everything.

    The Schematron can make assertions about patterns of occurrences (or occurrences of patterns, depending on your perspective) anywhere in the document because you can put any XPath in the test. This Schematron is looking both across and down the document to check that the number of sibling segtitle elements matches the number of seg elements of the current seglistitem. That's sorta out of scope for a language, like RNG, that defines the allowed structure of the document, but you'd also find defining the allowed structure using only Schematron to be verbose and cumbersome.

    Using RNG and Schematron in combination like this uses the strengths of each. (You can also do much the same with assertions in XSD 1.1.)