【问题标题】:python xmlschema identifying all nested attributespython xmlschema 识别所有嵌套属性
【发布时间】:2020-10-07 02:32:06
【问题描述】:

我正在尝试遍历 xml 架构。目标是打印出所有复杂类型及其嵌套组件。此 xsd 具有嵌套的复杂类型。

我正在使用“xmlschema”来执行此操作。在下面的代码中,我只能在一开始就挑选出复杂类型,但为了深入研究,我需要根据 element\validator 类型采取行动。我似乎无法指定一个声明,如果它是复杂类型,请执行此操作,或者如果它是简单类型,则执行其他操作:

import xmlschema

from pprint import pprint
schema = xmlschema.XMLSchema('Schema.xsd')
for t in schema.complex_types:
    print(t.name)
    for c in t.iter_components():
        print('\t', c.name)
        print('\t', type(c))

【问题讨论】:

    标签: python xsd schema python-xmlschema


    【解决方案1】:

    要遍历所有定义的复杂类型(全局和局部),您可以从架构实例中迭代组件并使用过滤返回的 XSD 组件类型的选项:

    import xmlschema
    from xmlschema.validators import XsdComplexType
    
    schema = xmlschema.XMLSchema('Schema.xsd')
    for xsd_type in t.iter_components(xsd_classes=XsdComplexType):
        print(xsd_type)
    

    【讨论】:

      猜你喜欢
      • 2018-10-28
      • 2022-06-28
      • 2014-02-07
      • 2020-08-08
      • 1970-01-01
      • 2012-08-11
      • 2013-07-22
      • 2015-10-17
      • 1970-01-01
      相关资源
      最近更新 更多