zenn1337
zenn1337

Reputation: 491

Apache Xerces get declaration of every element in xsd

I'm using Apache Xerces to parse the following XSD file.

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

I would like to get declarations of every element in the file, that is: Root, ElementA, ElementB and ElementC. The method:

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

returns null for ElementA, ElementB and ElementC. It only finds the declaration of the Root element.

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

does not work either, it returns only the Root declaration. How to get these declarations nested in the RootType?

Upvotes: 0

Views: 528

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

You have to search the schema component model recursively. From a global element declaration (XSElementDeclaration) getTypeDefinition() gets you to an XSComplexTypeDefinition. From there recursively using getParticle() and getTerm() will eventually get you to XSElementDeclaration objects representing the local element declarations.

(The Saxon SCM file contains essentially the same data structure but in an XML format, which allows you more convenient searching, using XPath expressions rather than step-by-step procedural navigation.)

Upvotes: 1

Related Questions