【问题标题】:Apache Xerces get declaration of every element in xsdApache Xerces 获取 xsd 中每个元素的声明
【发布时间】:2017-03-15 16:18:32
【问题描述】:

我正在使用 Apache Xerces 来解析以下 XSD 文件。

<xs:element name="Root" type="RootType">
    ...
</xs:element>

<xs:complexType name="RootType">
    <xs:complexContent>
        <xs:extension base="BaseElement">
            <xs:choice maxOccurs="unbounded">
                <xs:element name="ElementA" type="ElementAType" />
                <xs:element name="ElementB" type="ElementBType" />
                <xs:element name="ElementC" type="ElementCType" />
            </xs:choice>

            <xs:attribute ... >...</xs:attribute>
            <xs:attribute ... >...</xs:attribute>
            <xs:attribute ... >...</xs:attribute>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="BaseElement">
    ...
</xs:complexType>

<xs:complexType name="ElementAType">
    ...
</xs:complexType>

<xs:complexType name="ElementBType">
    ...
</xs:complexType>

<xs:complexType name="ElementCType">
    ...
</xs:complexType>

我想获得文件中每个元素的声明,即:Root、ElementA、ElementB 和 ElementC。方法:

XSElementDeclaration decl = model.getElementDeclaration("ElementA");

为 ElementA、ElementB 和 ElementC 返回 null。它只找到 Root 元素的声明。

XSNamedMap comp = model.getComponents(XSConstants.ELEMENT_DECLARATION);

也不起作用,它只返回 Root 声明。 如何让这些声明嵌套在 RootType 中?

【问题讨论】:

    标签: xml xsd nested element xerces


    【解决方案1】:

    您必须递归地搜索架构组件模型。从全局元素声明 (XSElementDeclaration) getTypeDefinition() 将您带到 XSComplexTypeDefinition。从那里递归地使用 getParticle() 和 getTerm() 最终将带您到表示本地元素声明的 XSElementDeclaration 对象。

    (Saxon SCM 文件包含基本相同的数据结构,但采用 XML 格式,这使您可以使用 XPath 表达式而不是逐步的程序导航更方便地进行搜索。)

    【讨论】:

    • 谢谢,我明白了!但是,我遇到了另一个问题(我已经编辑了我的问题)。
    • 请不要在得到答案后更改问题,这会使线程很难跟进。如果您还有其他问题,请在新帖子中提出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多