Reputation: 299218
I am creating an XML schema in which I would like to restrict node values by the types or values of their parent nodes. The thing is, I don't know the restrictions at the time of schema authoring, because the schema must be valid for more than one such application. So I would like to keep the value types in an external document (that will probably be created automatically before the document is edited).
My main purpose is to make editing the XML files easy with XML-savvy tools (preferably Eclipse), with auto-completion of allowed values.
Here's a fictional example. Let's say I want to create a group of characters from the same comic / cartoon.
<team ref="marvel-heros">
<member ref="spiderman" />
<member ref="hulk" />
<member ref="batman" /><!-- illegal, not in marvel-heros -->
</team>
<team ref="pacman-ghosts">
<member ref="blinky" />
<member ref="inky" />
<member ref="pinky" />
<member ref="clyde" />
<member ref="qbert" /><!-- illegal, not in pacman-ghosts -->
</team>
Other possible markups would be
<marvel-heroes>
<spiderman />
<hulk />
</marvel-heroes>
<pacman-ghosts>
<inky />
<pinky />
</pacman-ghosts>
Of course the markup can be changed, and namespaces could be used as well (although I'd rather not use one namespace per cartoon / comic as there are many).
Note that the marvel team and the pacman ghosts can appear multiple times in the same document.
Is there a sensible way I can do this? Do I have to create an external schema for the values? Or is there a way to solve this using entities or xml includes?
Upvotes: 1
Views: 149
Reputation: 2998
If you use XML Schema, you better use the second kind of markup (<marvel-team>...
). Only RelaxNG allows such validation mechanism given specific attribute values.
For your concern about external documents, maybe be you can consider the include mechanism in XML Schema :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="type.xsd"/>
<xs:element name="marvel-team" type="marvel-teamType"/>
<xs:element name="pacman-ghosts" type="pacman-ghostsType"/>
</xs:schema>
And for the type.xsd
:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="marvel-teamType">
<xs:sequence>
<xs:element name="spiderman" />
<xs:element name="hulk" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="pacman-ghostsType">
<xs:sequence>
<xs:element name="inky" />
<xs:element name="pinky" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Only type.xsd
has to change when you add or remove elements.
Upvotes: 1