Referencing specific element(s) in a RelaxNG schema with externalRef
-
27-04-2021 - |
Question
So I have one RelaxNG schema that references another:
<define name="review">
<element name="review">
<externalRef href="other.rng"/>
</element>
</define>
other.rng:
<start>
<choice>
<ref name="good"/>
<ref name="bad"/>
</choice>
</start>
<define name="good">
<element name="good"/>
</define>
<define name="bad">
<element name="bad"/>
</define>
Is there any way I can import only <good>
, but not allow <bad>
? The goal being:
<review><good/></review>: valid
<review><bad/></review>: invalid
Solution
The grammar you import with externalRef
can't be modified. To achieve the kind of validation you're after, I see this method :
<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<include href="other.rng">
<start combine="choice">
<ref name="review"/>
</start>
</include>
<define name="review">
<element name="review">
<ref name="good"/>
</element>
</define>
</grammar>
- You include the other schema.
- You override the
start
element in the include (good
andbad
elements won't be possible root). The specification says :
If the include element has a start component, then all start components are removed from the grammar element.
- You make a reference to the
good
element in yourreview
definition.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow