2

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;
Peter Wolf
  • 3,265
  • 1
  • 10
  • 27
Bosshoss
  • 368
  • 1
  • 13

1 Answers1

2

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;
Peter Wolf
  • 3,265
  • 1
  • 10
  • 27
  • Thanks you, I can't edit the xsd that's not mine, so the generated class is good :) – Bosshoss Sep 30 '20 at 11:53
  • @Bosshoss Note that I generated the code from self-made XSD and the generated interface inherits from `IXMLNodeCollection`. In your case it was `IXMLNode`, because the type is a part of a bigger schema that you didn't provide. You can edit vendor XSD to adapt it to your needs, but keep in mind that XML generated by your cone may not meet the original schema. – Peter Wolf Sep 30 '20 at 11:58