XSD: Allow Elements From Another Namespace In Any Order

I've tried the following. I want to create an XSD for XML where some elements can only appear once and must be valid, and elements from other namespaces are allowed anywhere and have no schema to validate against.

XML to 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 XML and must be validated, elements in other namespaces followed by ns: must be allowed without validation. For this I am using 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, elements in other namespaces can only appear after a sequence, and not between required elements. The ideal solution (but I know this is not allowed in XSD) is to change my xs: sequence to xs: all (but xs: any is not allowed in xs: all)

I know there are such questions, but none of the answers there are clear. Can anyone suggest 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>

      

Since unlimited comes from choice, any item from the choice can happen as many times as they want in any position. But here book1, 2, 3 and 4 may appear several times, and in my case, use is not allowed.

Does anyone have any other idea that might help me? Thanks in advance!

PS: The types of my books are actually complex. The types are all different from each other, it's just a simplified version of XML.

EDIT:

This is not allowed in my XSD either:

   <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>

      

+3


source to share


1 answer


In XSD 1.1, you can do this very easily by specifying an open content model:

<xs:complexType ...>
  <xs:openContent mode="interleave">
    <xs:any namespace=.../>
  </xs:openContent>
  ... regular content model ...
</xs:complexType>

      



There is no easy way to do this in 1.0; you just need to add an extra template at any position you can.

+1


source







All Articles