我已经使用 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
但是如果有 2 <Nm>
呢? XML 中的节点,如何访问它们?生成的接口(interface)仅支持单个<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
以及 minOccurs
是 1
.
允许多个 <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>
生成的接口(interface)是:
{ 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;
关于xml - 来自 XSD 的 Delphi 读取器 XML 可选值 0..n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64135785/