generateDS停止处理子元素

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (71)

我有一个描述电子发票结构的xml架构。我已经使用schema与generateDS来为该格式创建解析器。在解析发票时,它似乎工作正常,但是任何类型内容的部分,它会停止处理这些元素出现的子项。

描述任何元素的模式的一部分:

<!-- Elements to describe the invoice extensions -->
    <xs:complexType name="ExtensionRecord">
        <xs:sequence>
            <xs:element name="InformationName" type="NormalTextType" minOccurs="0"/>
            <xs:element name="InformationContent" type="LongTextType"/>
            <xs:element name="CustomContent" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:any processContents="skip"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="extensionId" type="ShortTextType" use="optional"/>
    </xs:complexType>

我的实现的相关部分使用解析器:

E_Invoice = einvoice111.parseString(xmlString, silence=True)

for ai in E_Invoice.Invoice.AdditionalInformation:
    print(dir(ai) )
    print(dir(ai.CustomContent))
    print(ai.CustomContent.export(sys.stdout, 0, name_='CustomContent'))

有效负载XML的一部分:

    <AdditionalInformation extensionId="invoicePDFFormat">
      <InformationContent/>
      <CustomContent>
        <any>
          <Content>JVBERi0xLjQ........
                    <BASE64 coded binary>
        .....</Content>

和该代码的输出:

['CustomContent', 'InformationContent', 'InformationName', 'Tag_strip_pattern_', '_FixedOffsetTZ', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'build', 'buildAttributes', 'buildChildren', 'convert_unicode', 'export', 'exportAttributes', 'exportChildren', 'extensionId', 'factory', 'gds_build_any', 'gds_encode', 'gds_format_base64', 'gds_format_boolean', 'gds_format_boolean_list', 'gds_format_date', 'gds_format_datetime', 'gds_format_double', 'gds_format_double_list', 'gds_format_float', 'gds_format_float_list', 'gds_format_integer', 'gds_format_integer_list', 'gds_format_string', 'gds_format_time', 'gds_parse_date', 'gds_parse_datetime', 'gds_parse_time', 'gds_reverse_node_mapping', 'gds_str_lower', 'gds_validate_base64', 'gds_validate_boolean', 'gds_validate_boolean_list', 'gds_validate_date', 'gds_validate_datetime', 'gds_validate_double', 'gds_validate_double_list', 'gds_validate_float', 'gds_validate_float_list', 'gds_validate_integer', 'gds_validate_integer_list', 'gds_validate_simple_patterns', 'gds_validate_string', 'gds_validate_time', 'get_CustomContent', 'get_InformationContent', 'get_InformationName', 'get_class_obj_', 'get_extensionId', 'get_path_', 'get_path_list_', 'hasContent_', 'original_tagname_', 'set_CustomContent', 'set_InformationContent', 'set_InformationName', 'set_extensionId', 'subclass', 'superclass', 'tzoff_pattern', 'validate_LongTextType', 'validate_NormalTextType', 'validate_ShortTextType']
['Tag_strip_pattern_', '_FixedOffsetTZ', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'anytypeobjs_', 'build', 'buildAttributes', 'buildChildren', 'convert_unicode', 'export', 'exportAttributes', 'exportChildren', 'factory', 'gds_build_any', 'gds_encode', 'gds_format_base64', 'gds_format_boolean', 'gds_format_boolean_list', 'gds_format_date', 'gds_format_datetime', 'gds_format_double', 'gds_format_double_list', 'gds_format_float', 'gds_format_float_list', 'gds_format_integer', 'gds_format_integer_list', 'gds_format_string', 'gds_format_time', 'gds_parse_date', 'gds_parse_datetime', 'gds_parse_time', 'gds_reverse_node_mapping', 'gds_str_lower', 'gds_validate_base64', 'gds_validate_boolean', 'gds_validate_boolean_list', 'gds_validate_date', 'gds_validate_datetime', 'gds_validate_double', 'gds_validate_double_list', 'gds_validate_float', 'gds_validate_float_list', 'gds_validate_integer', 'gds_validate_integer_list', 'gds_validate_simple_patterns', 'gds_validate_string', 'gds_validate_time', 'get_anytypeobjs_', 'get_class_obj_', 'get_path_', 'get_path_list_', 'hasContent_', 'original_tagname_', 'set_anytypeobjs_', 'subclass', 'superclass', 'tzoff_pattern']
<CustomContent/>

CustomContent有omittag,表明对象结构在那里结束。我也试过export()整个文档,它有相同的情况。

所以这是部分:

    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="skip"/>
      </xs:sequence>
    </xs:complexType>

这不会出现在Python对象树中。

当我查看与模式匹配的生成库时,CustomContent类的相关部分:

    def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
        obj_ = self.gds_build_any(child_, 'CustomContentType')
        if obj_ is not None:
            self.set_anytypeobjs_(obj_)

它使用gds_build_any()方法,而不是创建它将从模式生成的类的新实例(也不存在)。

使用Suds时,我可以访问任何元素及其内容,但它会在其他地方中断。

有没有办法配置generateDS,以便它:

  • 为所有模式的元素创建类?
  • 将这些类用于整个文档?
提问于
用户回答回答于

好吧,至少看来第1652行的generateDS.py SAX-handler XschemaHandler测试:

    if name == AnyType:
        element = XschemaElement(attrs)
        element.type = AnyTypeIdentifier
        self.stack.append(element)

和maches to tag(AnyType包含'xs:any')。所以GDS知道任何标签都是特殊的XML语法,如'sequence','simpleType','complexType'等。

这可能是它没有尝试为它创建一个类并将其映射到'any'的原因。

我修改了一下:

    elif name == AnyType:
        print('opening AnyType')
        element = XschemaElement(attrs)
        element.name = 'any'
        #element.type = 'NormalTextType'
        element.type = 'StringType'
        self.inAnyType = 1
        self.stack.append(element)

并且类似于sax结束标记,并将其添加到标记内容prosessing部分:

    def characters(self, chrs):
        if self.inDocumentationType:
            # If there is an annotation/documentation element, save it.
            if len(self.stack) > 1 and len(chrs) > 0:
                self.stack[-1].documentation += chrs
        elif self.inAnyType:
            if len(self.stack) > 1 and len(chrs) > 0:
                self.stack[-1].any += chrs
        elif self.inElement:
            pass

现在我输出为:

    <CustomContent>
        <any>

              </any>
    </CustomContent>

关闭但没有雪茄。

扫码关注云+社区

领取腾讯云代金券