简体   繁体   English

python xmlschema 标识所有嵌套属性

[英]python xmlschema identifying all nested attributes

I am trying to traverse through an xml schema.我正在尝试遍历 xml 模式。 The goal is to print out all the complextypes with their nested components.目标是打印出所有复杂类型及其嵌套组件。 This xsd has nested complextypes.此 xsd 具有嵌套的复杂类型。

I am using 'xmlschema' to do this.我正在使用“xmlschema”来执行此操作。 In the below code I could only pick out the complextypes in the begining but in order to drill down I need to take actions based on the element\validator type.在下面的代码中,我只能在一开始就挑选出复杂类型,但为了深入研究,我需要根据 element\validator 类型采取行动。 I can't seem to specify a statement which says if it is a complextype do this or if it is simpletype then do something else:我似乎无法指定一个语句,如果它是复杂类型,则执行此操作,或者如果它是简单类型,则执行其他操作:

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))

To traverse all the complex types defined (globals and locals) you can iterate components from the schema instance and use the option that filters the XSD component types returned:要遍历定义的所有复杂类型(全局和本地),您可以从模式实例中迭代组件并使用过滤返回的 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM