0

Suppose I have the following XSD file :

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"  elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="PACIDemoSignedDoc" type="PACIDemoSignedDocType" />
    <xs:complexType name="PACIDemoSignedDocType">
        <xs:all>
            <xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />
        </xs:all>
    </xs:complexType>
    <xs:complexType name="OwnerEnglishNameType">
        <xs:simpleContent>
            <xs:restriction base="NameType">
                <xs:enumeration value="John"/>
                <xs:enumeration value="Jack"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="NameType">
        <xs:simpleContent>
            <xs:extension base="xs:string"/>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>

My question is, where should I add the attributes minOccurs and maxOccurs for the element "OwnerEnglishName" ? I want to keep xs:all as I want to avoid having to order my XML file in sequence but it prohibits me from adding the Occurs directly in the <xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" /> line...

I'm also guessing I can't add the Occur attributes inside OwnerEnglishNameType, anyone have any idea ?

Yohann
  • 1
  • 3
  • Who is it that "prohibits [you] from adding"? I can't see, why `` (or similar) shouldn't work. – xystum Jul 22 '16 at 12:51
  • I guess you mean the error `F [Saxon-EE 9.4.0.4] Within , an must have @maxOccurs equal to 0 or 1`. Please specify, which `minOccurs` and `maxOccures` values you want to have. – xystum Jul 22 '16 at 12:57
  • Exactly, I wanted to add occur values that differ from 0 and 1 : I have no value in mind, I just have to be able to input any value for these attributes. – Yohann Jul 26 '16 at 08:28

2 Answers2

1

My question is, where should I add the attributes minOccurs and maxOccurs for the element OwnerEnglishName ?

On the (non-global) declaration of OwnerEnglishName:

<xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" 
            minOccurs="0" maxOccurs="1"/>

I want to keep xs:all as I want to avoid having to order my XML file in > sequence

In XSD 1.0, you cannot have maxOccurs="unbounded" under xs:all; in XSD 1.1, you can.

However, you don't need xs:all with a single child element; you can use xs:sequence since there's no second element for order to matter.


Update (per OP change of example in comments to include additional child elements to xs:all):

You then have three options:

  1. Impose an ordering. This is nearly always the best answer as the perceived need to allow any ordering is almost always unnecessary in practice.
  2. Use XSD 1.1, which allows children of xsd:all to have maxOccurs="unbounded".
  3. Change the XML design to use a wrapper element around the child element of xsd:all which you wish to allow to repeat. (See here for an example.)
Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • The problem was that, in the end, I'll have more than one child element. Are there really no workaround ? I've been desperately trying to make something work like this : So basically defining sequences as children of "all" elements to bypass the Occur restriction. Am I struggling to do something that is ultimately pointless or do you think such a workaround could work ? – Yohann Jul 26 '16 at 08:30
  • Then your example should have shown multiple child elements of `xsd:all`, not a single one. You have three options: See **[Unordered elements in XSD with obligatory and unbounded elements?](http://stackoverflow.com/questions/37969890/unordered-elements-in-xsd-with-obligatory-and-unbounded-elements)** – kjhughes Jul 26 '16 at 12:12
-1

Okay, I have solved my problem, here's the answer if others have the same problem :

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" targetNamespace="test" xmlns="test" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="animal" type="AnimalType" />
    <xs:complexType name="AnimalType">
        <xs:all>
            <xs:element name="cats" type="Cats" />
        </xs:all>
    </xs:complexType>    
    <xs:simpleType name="CatType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="John"/>
            <xs:enumeration value="Jack"/>
        </xs:restriction>
    </xs:simpleType>    
    <xs:complexType name="Cats">
        <xs:sequence>
            <xs:element maxOccurs="5" minOccurs="2" name="cat" type="Cat"/>
        </xs:sequence>
    </xs:complexType>    
    <xs:complexType name="Cat">
        <xs:simpleContent>
            <xs:extension base="CatType"/>
        </xs:simpleContent>
    </xs:complexType>    
</xs:schema>

which validates the following xml :

<?xml version="1.0" encoding="UTF-8"?>
<animal xmlns="d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="test schema.xsd">
    <cats>
        <cat>John</cat>
        <cat>Jack</cat>
    </cats>
</animal>

I have successfully bypassed the Occurs restriction of xs:all by adding child elements following xs:sequence.

Yohann
  • 1
  • 3
  • *I have successfully bypassed the Occurs restriction of `xs:all`*: **No, you haven't.** You've **(1)** changed the example (why?), **(2)** still used `xs:all` with a single child element (which buys you nothing), and **(3)** applied a [well-known wrapper technique](http://stackoverflow.com/questions/37969890/unordered-elements-in-xsd-with-obligatory-and-unbounded-elements) (which doesn't bypass the restriction but changes the XML design to work within it). – kjhughes Jul 26 '16 at 11:56