【问题标题】:how to set an inner nillable element to null如何将内部 nillable 元素设置为 null
【发布时间】:2015-04-27 18:12:43
【问题描述】:

我正在尝试将一个名为 nbReports 的内部 nillable 元素设置为 null,但到目前为止我失败了。

XSD 文件的快照,接下来是代码:

<xs:element name="reports" type="tns:Reports"/>
<xs:complexType name="Reports">
  <xs:sequence>
     <xs:element minOccurs="0" name="description" type="xs:string"/>
     <xs:element minOccurs="0" name="reportingGroups">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" minOccurs="0"
                    name="reportingGroup" type="tns:ReportingGroupType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
  </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ReportingGroupType">
  <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="nbReports" type="xs:int" nillable="true"/>
  </xs:sequence>
  <xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>

代码截图:

import reports
rep=reports.reports()
rep.description="this report is ..." 
temp=ReportingGroupType(id=xs.string("A1")
temp.title="this title")
temp.nbReports._setIsNil #"error: AttributeError: 'NoneType' object has no attribute  '_setIsNil'

如果我尝试将 nbReports 设置为任何值(例如 4)以创建 nbReprots 的实例,然后将其设置为 null,则打印时 nbReprots 值将保持为 4。

【问题讨论】:

  • 对于 XSD 中的 nillable="true" 是 XML 文件中带有 nil="true" 的节点上的属性的节点,我收到的信息。请注意,它是一个属性,而不是节点的内容。例如,如果我有一个 &lt;xs:element name="void_date" nillable="true"/&gt; 的 XSD 声明,那么我收到的 XML 是 &lt;void_date nil="true"/&gt;
  • 我明白你的意思。我的下一个问题是,如果我的元素 nbReports 是 reportingGroupType 的子元素,如何将 nil 属性设置为 true?

标签: xml xsd pyxb


【解决方案1】:

首先你需要将元素绑定到pyxb.BIND()

基于 Ken 的 cmets,我能够将 nil 属性设置为 true :

temp.nbReports=pyxb.BIND()
temp.nbReports._setIsNil(nil=True)

感谢您的帮助。

马莉卡

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2018-10-24
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多