2
votes

I have generated a unit from XSD file with the XML wizard.

All is OK, but I have an optional node with minOccurs="0" without max.

<xs:complexType name="Party">
  <xs:sequence>
    <xs:element name="Nm" type="Max70Text" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>

In Delphi code I can access the value with:

LDocument.Aaa.Bbb.Items[0].Xxx.Nm

But what if there are 2 <Nm> nodes in the XML, how can I access them? The generated interface supports just single <Nm> node.

IXMLParty = interface(IXMLNode)
  { Property Accessors }
  function Get_Nm: UnicodeString;
  procedure Set_Nm(Value: UnicodeString);
  { Methods & Properties }
  property Nm: UnicodeString read Get_Nm write Set_Nm;
end;
1

1 Answers

2
votes

Your assumption that omitting the maxOccurs attribute in element's definition would allow more than one <Nm> element is wrong. The default value for maxOccurs as well as minOccurs is 1.

To allow multiple <Nm> element you have to explicitly specify maxOccurs="unbounded" in your schema (I replaced Max70Text type with generic xs:string):

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified"
  xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="Party">
    <xs:sequence>
      <xs:element name="Nm" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

The generated interface is:

{ IXMLParty }

IXMLParty = interface(IXMLNodeCollection)
  { Property Accessors }
  function Get_Nm(Index: Integer): UnicodeString;
  { Methods & Properties }
  function Add(const Nm: UnicodeString): IXMLNode;
  function Insert(const Index: Integer; const Nm: UnicodeString): IXMLNode;
  property Nm[Index: Integer]: UnicodeString read Get_Nm; default;
end;