简体   繁体   中英

map xml element to xsd complexType based on attribute

Assume there exists an XML instance document that looks like this:

<root>
    <object type="foo">
        <!-- ... -->
    </object>

    <object type="bar">
        <!-- ... -->
    </object>
</root>

My goal is to have a small (static) schema that verifies proper <element type="xxx" /> syntax for objects, and another schema (more prone to change) that verifies the contents of each object element against a complexType that matches the type attribute:

<complexType name="foo"><!--should match object with type="foo"--></complexType>
<complexType name="bar"><!--should match object with type="bar"--></complexType>

What is the best way to accomplish this (or something similar)?

There's no way to do this with pure XML Schema.

You could validate using the "static" schema, then for each element, validate it against the specific complexType that it should match, not against the entire schema.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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