I've been trying out the following. I want to create an XSD for an XML where certain elements are allowed to appear only once and need to be valid, and elements from other namespaces are allowed anywhere, and have no schema they have to validate with.
XML that should be allowed:
<ns:bookstore>
<ns:books>
<ns:book1 />
<other:magazine1 />
<ns:book2 />
<ns:book3 />
<otherns:newspaper1 />
<ns:book4 />
</ns:books>
</ns:bookstore>
book1,2,3 and 4 can only appear once in the XML and need to be validated, elements in other namespaces then ns: should be allowed without being validated. For this I use an xs:any with processContents lax in my XSD:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="ns">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence >
<xs:element name ="book1" type="xs:string" maxOccurs="1"/>
<xs:element name ="book2" type="xs:string" maxOccurs="1"/>
<xs:element name ="book3" type="xs:string" maxOccurs="1"/>
<xs:element name ="book4" type="xs:string" maxOccurs="1"/>
<xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
In this solution, the elements in other namespaces can only appear after the sequence, and not in between the obligatory elements. The ideal solution (but I know it's not allowed in XSD) is to change my xs:sequence in an xs:all (but xs:any is not allowed in xs:all)
I know there are questions like this, but none of the answers there are clear to me. Can someone offer me a workaround for this problem?
I've also tried the following:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="ns">
<xs:element name="bookstore">
<xs:complexType>
<xs:choice maxOccurs="unbounded" >
<xs:element name ="book1" type="xs:string" maxOccurs="1"/>
<xs:element name ="book2" type="xs:string" maxOccurs="1"/>
<xs:element name ="book3" type="xs:string" maxOccurs="1"/>
<xs:element name ="book4" type="xs:string" maxOccurs="1"/>
<xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
</xs:element>
Because the unbounded occurs of the choice, any element from the choice can occur as many times as they want at any position. But here book1, 2, 3 and 4 can occur multiple times, with is not allowed in my use case.
Does anybody have another idea that might help me? Thanks in advance!!
P.S.: The types of my books are in fact complexTypes and all different from eachother, this is just a simplified version of the XML.
EDIT:
This isn't allowed either in my XSD:
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence >
<xs:element name ="book1" type="xs:string" maxOccurs="1"/>
<xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name ="book2" type="xs:string" maxOccurs="1"/>
<xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name ="book3" type="xs:string" maxOccurs="1"/>
<xs:element name ="book4" type="xs:string" maxOccurs="1"/>
<xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>