简体   繁体   中英

XSD for alternative root elements from different namespaces?

XML Schema V1.0 can specify alternative root nodes for instances by way of the fact that there is no single root node definition:

https://stackoverflow.com/a/8857777/8254682

So it's possible to leverage this to provide a kind of "document type selection" without using XSD V1.1., based on which of several globally declared elements is used in the instance as the root node, right?

But can "alternative root nodes" be in different namespaces, as long as these namespaces are declared properly? Do element and attribute qualification matter in that case?

Any root element (that is not abstract) can be used as the definition for the root element in an XML document.

Furthermore you can keep the same root element name and change the type of the element using the xsi:type attribute in the XML document (the type used must be based on the type defined in the RootElm).

在此处输入图片说明

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2018 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="RootElm" type="RootType" />
    <xs:complexType name="RootType">
        <xs:sequence>
            <xs:element name="A" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="RootType1">
        <xs:complexContent>
            <xs:extension base="RootType">
                <xs:sequence>
                    <xs:element name="B" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="RootType2">
        <xs:complexContent>
            <xs:extension base="RootType">
                <xs:sequence>
                    <xs:element name="C" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

Sample XML Doc 1

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<RootElm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="C:\Temp\XSDFile1.xsd">
    <A />
</RootElm>

Sample XML Doc 2

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<RootElm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="C:\Temp\XSDFile1.xsd" 
         xsi:type="RootType1">
    <A />
    <B></B>
</RootElm>

Sample XML Doc 3

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<RootElm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="C:\Temp\XSDFile1.xsd" 
         xsi:type="RootType2">
    <A />
    <C />
</RootElm>

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