Subclass / Overload relax-ng definitions

Relax-ng is so great, you can subclass definitions to better match your needs.

Let’s say you’ve make a definition of an element in file elem1.rng as follow:

<define name="elem1.class">..def1..</define>

Now you want to modify this element .

Add extra choice

Let’s say you want elem1 to be able to match either def1 or another definition called def2.
To add a choice node, simply redefine this element in your current relax-ng file adding the ‘combine=”choice”‘ attribute, as follow:

<define name="elem1.class" combine="choice">..def2..</define>

Now it’s the same as having:

<define name="elem1.class"><choice><..def1..><..def2..></choice></define>

Add extra requested content

Let’s say now that you want to add another constraint to elem1, it needs to match both def1 AND def2.
Simply redefine this element in your current relax-ng file adding the ‘combine=”interleave”‘ attribute, as follow:

<define name="elem1.class" combine="interleave">..def2..</define>

Now it’s the same as having:

<define name="elem1.class"><interleave><..def1..><..def2..></interleave></define>

Replacing a definition

Now you don’t want elem1 to match def1 but simply to match def2, ie. you want to replace def1 with def2.
Here we don’t use any combine attribute, instead we simply redefine our element within the include node.

Typically include node is stand-alone as follow:

<include href="elem1.rng"/>

To subclass a definition, simply redefine your element within the include node as follow:

<include href="elem1.rng">
<define name="elem1.class">...def2...</define>
</include>

Now it’s as if elem1.rng had always contained the above definition refering def2.

sources

Post a Comment

Your email is never published nor shared. You're allow to say what you want...