1

The issue is that I am using XS:all because the elements can be in any order. I can not put a maxoccurs on a referenced element:

    <xs:element name="PersonInfo">
    <xs:complexType>
        <xs:all>
            <xs:element name="Addr" type="AdressType" minOccurs="0" maxOccurs="unbounded" />

        </xs:all>
        <xs:attribute name="id" type="xs:ID" use="optional" />
        <xs:attribute name="Test" type="xs:string" use="optional" />

    </xs:complexType>
</xs:element>

<xs:complexType name="AdressType">
    <xs:all>
        <xs:element name="BuildingAptNumber" type="xs:string" minOccurs="0" />
        <xs:element name="Addr1" type="xs:string" minOccurs="0" />
        <xs:element name="City" type="xs:string" minOccurs="0" />
        <xs:element name="StateProvCd" type="xs:string" minOccurs="0" />
        <xs:element name="PostalCode" type="xs:string" minOccurs="0" />
    </xs:all>
    <xs:attribute name="AddrTypeCd" type="xs:string" use="optional" />
</xs:complexType>

If the element was not referenced i would be able to get away with using a ComplexType. Is there any way to reference the element and have a maxoccurs under xs:all ?

EDIT: The only way I see to add maxoccurs is to use a complextype and define xs:choice inside of the complex type:

        <xs:element name="Test" minOccurs="0">
                <xs:complexType>
                    <xs:choice maxOccurs="unbounded">
                        <xs:element name="Addr" type="PMICADDRESS"/>
                    </xs:choice>
                </xs:complexType>

            </xs:element>

So the issue is that I don't know how to declare complex type of the addr element. The one solution I found is to change to xsd 1.1. I have not been able to update to XSD 1.1. yet. I found a thread that is helping out: Middle way between XSD all and XSD sequence

Community
  • 1
  • 1
james31rock
  • 2,615
  • 2
  • 20
  • 25

2 Answers2

2

The restriction to maxOccurs="1" on xs:all is removed in XSD 1.1. Do you have the option of using an XSD 1.1 processor? There are currently three, as far as I am aware: Saxon, Xerces, and Altova.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

If the element was not referenced i would be able to get away with using a ComplexType

No, you still could not have maxOccurs="unbounded" on an xs:element under xs:all.

Is there any way to reference the element and have a maxoccurs under xs:all ?

No, xs:element under xs:all may only be 0 or 1.


Update regarding xs:choice idea in comments:

I could use xs:choice and create the element instead of referencing an existing one (not ideal as reference is used multiple spots). I am trying to figure out if there is a way to use a reference to an element with xs:choice.

If your priority is to avoid ordering and still allow multiple Addr elements under PersonInfo, yes, you could use xs:choice with maxOccurs="unbounded":

  <xs:choice maxOccurs="unbounded">
    <xs:element name="Addr" type="AdressType"/>
    <xs:element name="e1"/>
    <xs:element name="e2"/>
  </xs:choice>

Note, however, that unlike with xs:all, here Addr or e1 or e2 could be omitted. In this particular configuration, e1 and e2 can also occur repeatedly. See minOccurs and maxOccurs on elements inside xsd:choice for how to interpret maxOccurs on xsd:choice and its xsd:element children if you wish to explore other combinations. Finally, you could segregate the xs:all elements before or after a sequenced set of elements where you could allow cardinality greater than 1.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • MSDN says, " Any value for the maxOccurs attribute other than 1 is invalid. Xsd.exe reports an error for an invalid value." https://msdn.microsoft.com/en-us/library/d3hx2s7e%28v=vs.85%29.aspx . I am also getting an error. I am using visual studio and C# code for XSD validation.. – james31rock Feb 24 '15 at 16:21
  • Your MSDN quote applies to the `xs:all` element itself, not its `xs:element` children, which may have `maxOccurs` of 0 or 1, as I stated. (Aside: please cite W3C, *not MSDN*, for XML standards discussions.) More to the point of your question, `unbounded` may not be used as a value of `maxOccurs` on `xs:element` under `xs:all` at all. Hope this helps. – kjhughes Feb 24 '15 at 16:38
  • Thanks, I understand what your saying, the XS:element is nested inside of xs:all. and the error is on xs:element. using xs;sequence would take care of the issue, but the elements could appear in any order (which is why I use xs:all).I understand maxOccurs can not be used on xs:element under xs:all (reason for this post in the first place). I was looking for an alternative. I could use xs:choice and create the element instead of referencing an existing one (not ideal as reference is used multiple spots). I am trying to figure out if there is a way to use a reference to an element with xs:choice – james31rock Feb 24 '15 at 16:59
  • I've updated the answer for you to address the possibility of using `xs:choice`. – kjhughes Feb 24 '15 at 21:41
  • thanks for the update, unfortunately I can't use xs:choice under xs:all without using a complex type. updated my question, with example and why it's an issue. – james31rock Feb 25 '15 at 15:41