I have multiple XML descriptor files in my actual project and I’m having troubles with creating corresponding XSDs.
I have my custom types in XML, for example:
<A>
<B>
...
</B>
</A>
I need to create template blocks in my XML file. These blocks may contain any elements of my custom types. However, there is a slight difference between an element in the templates and elements elsewhere: they may have a Parameter element
<Template>
<Parameter />
<A>
<Parameter />
<B>
<Parameter />
...
</B>
</A>
</Template>
As far as I know, it is impossible for to declare that the <Parameter> element can appear anywhere inside <Template> (including the child nodes). But I can do this by defining <Parameter> as an element of all of my type definitions.
However I want to restrict the <Parameter> so that it can only appear when its parent is inside a <Template> element. Is it possible to put the <Parameter> in a type definition but restrict its appearance to when its parent is in the scope of a <Template> tag?
How can I write this in XSD?
After investigation I don’t believe this is possible. There are a number of relevant questions on SO that relate to this, and the general advice is that elements of an XSD can’t be dependent on other elements of an XSD.
Instead, you may need to consider some alternative approaches or workarounds.
Without knowing your requirement it’s difficult to consider what else might work for you, but perhaps you could tie the
TemplateandParametermore closely together, and less closely with the other elements. Foe example, aTemplatemight have aParameter, anotherTemplate, and an element (theA,B, etc):Define a
Templatelike this:And then each element does not need to be modified, but instead the
Templatetype stores the structure.