Reputation: 69
I have an XML -Data with the following structure
<Data>
<Name />
<Code />
<Prodprop>
<key />
<value />
</Prodprop>
<Prodprop>
<key />
<value />
</Prodprop>
<Tag />
<Blub />
</Data>
I need an XML Schema for this data , but the tags can appear in any order, but all Prodprop are consecutive. All other elements are needed exactly once or maximal once Therefore the following data is also valid.
<Data>
<Code />
<Name />
<Tag />
<Prodprop>
<key />
<value />
</Prodprop>
<Prodprop>
<key />
<value />
</Prodprop>
</Data>
This i my (and i know that this is not possible) Schema.
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Data" type="DataType" />
<xsd:complexType name="DataType">
<xsd:all>
<xsd:element name="Name" type="xsd:string" />
<xsd:element name="Code" type="xsd:string" />
<xsd:element maxOccurs="unbounded" name="Prodprop" type="ProdpropType" />
<xsd:element name="Tag" type="xsd:string" />
<xsd:element name="Blub" type="xsd:string" />
</xsd:all>
</xsd:complexType>
<xsd:complexType name="ProdpropType">
<xsd:sequence>
<xsd:element name="key" type="xsd:string" />
<xsd:element name="value" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Is there a possibility to make for this szenario a valid xml schema ?
Upvotes: 2
Views: 1585
Reputation: 17735
I think what you are trying to do is not possible without using something complicated in xpath.
@C. M. Sperberg-McQueen is right. It is possible, but, if you won't change the requirements to something simpler, you would have to provide all the possible combinations of the order that the tags might have. This solution though is very complicated and troublesome, although perfectly legit.
A good idea, close to what you describe, is to wrap Prodprop elements in another element e.g. ProdpropList. With this approach you will have all Prodprop items as a batch and whenever they are moved, they are moved as a whole.
You can test it online here
Check this: XSD
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Data" type="DataType" />
<xsd:complexType name="DataType">
<xsd:all>
<xsd:element name="Name" type="xsd:string" />
<xsd:element name="Code" type="xsd:string" />
<xsd:element name="ProdpropList" type="ProdpropListType" />
<xsd:element name="Tag" type="xsd:string" />
<xsd:element name="Blub" type="xsd:string" />
</xsd:all>
</xsd:complexType>
<xsd:complexType name="ProdpropListType">
<xsd:sequence>
<xsd:element name="Prodprop" type="ProdpropType"
maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ProdpropType">
<xsd:sequence>
<xsd:element name="key" type="xsd:string" />
<xsd:element name="value" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
XML
<Data>
<Name />
<Code />
<ProdpropList>
<Prodprop>
<key />
<value />
</Prodprop>
<Prodprop>
<key />
<value />
</Prodprop>
</ProdpropList>
<Tag />
<Blub />
</Data>
Upvotes: 3
Reputation: 25034
If I understand your question correctly, you want the content of the Data element to contain
The answers which claim that this is impossible are incorrect. The content model you describe is not impossible; it is clearly a regular language and is clearly expressible with an XSD content model. It is certainly possible; it is however extremely inconvenient.
To meet the requirements you state, you will need to create a regular expression for the language (the allowable sequences of children) you describe, and then write that regular expression down as an XSD content model. The task is complicated by XSD's quixotic attempt to keep content models 'simple' by requiring that they be 'deterministic', but you can find descriptions of how to go about in other StackOverflow questions here, here, and here (the last question is about DTDs, not XSD, but the same principles apply).
If you don't want to have a complicated content model, then you can achieve a much simpler one by revising your requirements so they fit your validation technology better.
all
group.Upvotes: 2