Delphi reader XML 来自 XSD 可选值 0..n

Delphi reader XML from XSD optional value 0..n

我已经使用 XML 向导从 XSD 文件生成了一个单元。

一切正常,但我有一个可选节点 minOccurs="0" 没有最大值

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

在 Delphi 代码中,我可以通过以下方式访问值:

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

但是如果 XML 中有 2 个 <Nm> 节点,我该如何访问它们呢?生成的界面仅支持单个 <Nm> 节点。

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;

您认为在元素定义中省略 maxOccurs 属性将允许多个 <Nm> 元素的假设是错误的。 The default value for maxOccurs 以及 minOccurs1.

要允许多个 <Nm> 元素,您必须在架构中明确指定 maxOccurs="unbounded"(我将 Max70Text 类型替换为通用 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>

生成的界面为:

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