Design Patterns

Design Patterns

In this section I'd like to add questions about the general design of schemas, good design ideas, commenting schemas etc.

1. How to insert comments
2. Entities and XInclude
3. Managing entities
4. Using the list pattern
5. Standalone and modular schemas.

1.

How to insert comments

DaveP

With some schema its difficult to add structured comments. Relaxng facilitates this using the <a:documentation> element. All descendents of this element are presumed to be in the same namespace, which it is not necessary to declare, and can be extracted to provide separable documentation.

The following uses this element, and some descendents from the HTML schema to provide better documentation.

 <define name="Mdate">
  <a:documentation>
   <p>The date format used in the XYZ project</p>
  </a:documentation>
 <element name="mdate">
   <a:documentation>
    <p>The element name is mdate</p>
   </a:documentation>
  <list>
    <ref name="Dow"/>
    <ref name="Month"/>
    <ref name="Dom"/>
    <ref name="Time"/>
    <ref name="Tz">
     <a:documentation>
      <p>The Tz part is the TimeZone, expressed as 
            a time offset from UTC</p>
     </a:documentation>
    </ref>
   </list>
  </element>
 </define>
     

In the example, the documentation is provided as the first child of the element or item being described. This makes for more regular processing with XSLT.

2.

Entities and XInclude

James Clark, Steinar Bang


> - Don't use entities to reference the chapters.  Instead use an
> element, i.e. XInclude. Either don't validate book.xml, or use a
> custom schema that allows the XInclude elements.  The processing of
> book.xml will have to resolve the XInclude's. (I think there are
> some XSLT processors that can do this already.)

xsltproc supports XInclude out of the box (it's what I currently use). Saxon and Xalan can be made to use XInclude. See Bob Stayton's book for more info: sagehill.net

3.

Managing entities

Michael Smith


> Under my DTD-based DocBook toolset, I was accustomed to being able to 
> declare entities inside the DOCTYPE declaration of a document.

[...]

>     <!DOCTYPE ...
>       [ <!ENTITY prod "HypeMaster"> ]>

You can do the same thing with nXML and RELAX NG. If you need to use entities in a particular document, just put the DOCTYPE in the document, with the entities in the internal subset, the same way you would if you were validating against a DTD. RNG tools will just read the internal DTD subset and ignore the (external) DTD.

Of course that means you then need to put DOCTYPE declarations in all your doc instances. But if you want to use entities, that's the tradeoff. There's currently no alternative.


> So, now that we've done away with the DOCTYPE, what's the substitute?
> I'm rather well behind the cutting edge in XML technology generally.
> Is there some obvious technique I'm missing, or maybe a new XProposal 
> that I haven't incorporated yet?

There's a least one proposal around -

Entity Definition Markup Language talsever.org

4.

Using the list pattern

John Cowan


> I want to specify a date as DD/MM/YYYY
> Can I use the list pattern for this

What you are trying to do is not possible. You need to use a WXS pattern instead.

The list pattern can only cope with whitespace-separated items, not with arbitrary concatenations.

5.

Standalone and modular schemas.

David Tolpin

I'm soliciting advise on how best to implement Relax modules that are both usable "standalone" and usable as modules in larger modules and larger standalone schemas.

My first thought was to give each module its own start pattern. So, lets say I have modules A and B that I wish to use standalone and also as parts of a bigger module AB.

1) I give each module a start pattern.

2) When I include a module in a larger module, I override its start pattern

Example:

File A.rnc - a standalone schema and a module
         A = element a { text }
         start = A

File B.rnc - a standalone schema and a module
         B = element b { text }
         start = B

File AB.rnc - a standalone schema and a module. It includes two sub-modules, A and B

         include "a.rnc" { start = AB }
         include "b.rnc" { start = AB }

         AB = element ab { A , B }
         start = AB

However, trang complains with this approach, when generating XSD's: - multiple definitions of "#start" without "combine" attribute

David T answers,

File AB.rnc

include "a.rnc" {start|=notAllowed}
include "b.rnc" {start|=notAllowed}

AB = element ab {A, B}