Parse XML schema with imported namespaces

Viewed 74

I have a complex XML schema consisting of multiple namespaces defined in different files. Below is a figure created in XSD Diagram that shows a small part of the schema.

Tree View of the Schema in XSD Diagram

Note that party is of type partyType, which is defined in a different namespace than parties. The (simplified) XML for parties in the main file (veg.xsd) looks like this:

...
<xsd:element name="parties" minOccurs="0">
    <xsd:annotation>
        <xsd:documentation>...</xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="party" type="misc:partyType" maxOccurs="unbounded">
                <xsd:annotation>
                    <xsd:documentation source="EML" xml:lang="en">...</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
...

Now, my aim is to derive the hierarchy of elements for the entire schema (across namespaces), pretty much like in the figure above. However, when I parse veg.xsd in R, I have no access to the imported namespaces. For example, when list all elements having a name attribute, I only get the names of elements defined in veg.xsd.

library(xml2)
def_xsd = read_xml("vegx_schema/veg.xsd")
xml_find_all(def_xsd, "//xsd:element") %>% 
  xml_attr("name") %>%
  length()

[1] 52 # Should be much larger

Any help appreciated!

1 Answers

Processing source schema documents is not easy, as you're discovering.

It's much better to process a compiled schema via an API.

I don't know what libxml2 offers in this way. If you can use Java then Xerces offers a Java API to the compiled schema, and Saxon allows you to export the compiled schema as an SCM file (which is XML, but unlike the source schema it's all assembled into a single document that's much easier to process).

Related