【问题标题】:Delphi reader XML from XSD optional value 0..n来自 XSD 可选值 0..n 的 Delphi 阅读器 XML
【发布时间】:2020-09-30 10:36:43
【问题描述】:

我已经使用 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 个&lt;Nm&gt; 节点,我该如何访问它们呢?生成的接口只支持单个&lt;Nm&gt; 节点。

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;

【问题讨论】:

    标签: xml delphi xsd


    【解决方案1】:

    您认为在元素定义中省略maxOccurs 属性将允许多个&lt;Nm&gt; 元素的假设是错误的。 The default value for maxOccurs 和 minOccurs 是 1。

    要允许多个&lt;Nm&gt; 元素,您必须在架构中明确指定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;
    

    【讨论】:

    • 谢谢,不是我的xsd我不能编辑,所以生成的类很好:)
    • @Bosshoss 请注意,我从自制的 XSD 生成代码,生成的接口继承自 IXMLNodeCollection。在您的情况下,它是IXMLNode,因为该类型是您未提供的更大架构的一部分。您可以编辑供应商 XSD 以使其适应您的需求,但请记住,您的锥体生成的 XML 可能不符合原始架构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2013-07-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多