pyxb将值分配给具有已知类型的未绑定内部复杂元素

时间:2015-02-06 18:28:09

标签: python pyxb

我正在尝试将值分配给预定义类型的未绑定内部元素,但我无法对其进行验证。 这是我的reports.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 minOccurs="0" name="description" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>

import reports
rep=reports.reports()

rep.description="this report is ..." #works

rep.reportingGroups=pyxb.BIND()

rep.reportingGroups.append("ReportingGroupType(name='this title",id=xs.string("A1")) #works

print(rep.reportingGroups.toxml("utf-8"))  #does not work

我无法找到与此类似的案例。我感谢您的帮助。 玛莉卡

1 个答案:

答案 0 :(得分:0)

如果您解释“不起作用”对您意味着什么,那将会有所帮助。

这一行:

rep.reportingGroups.append("ReportingGroupType(name='this title",id=xs.string("A1")) #works

很奇怪,对我不起作用,产生“无效的非元素内容”错误,因为字符串ReportingGroupType(name='this title不能解释为ReportingGroupType类型的元素。

如果我用对象构造函数替换该字符串并更正其工作的从属元素的名称:

rep.reportingGroups.append(reports.ReportingGroupType(title='not name', id='A1'))