none
How to validate xs:anyType element in XML with external schema

    General discussion

  • I have the following VehicleStatus.xsd schema has an element GPSStatus with anyType, so that I can assign a complex type pdal:GPSStatus in another schema GPSStatus.xsd

    VehicleStatus.xsd:

    <?xml version="1.0" encoding="UTF-8"?>

    <!-- edited with XMLSpy v2012 rel. 2 (http://www.altova.com) by Yiping Zou (Department of National Defense) -->

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pdal="http://mycompany.com/PDAL" targetNamespace="http://mycompany.com/PDAL" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:include schemaLocation="LES/PdalLESStatus.xsd"/>

     <xs:element name="VehicleStatus">

    <xs:complexType>

    <xs:sequence>

      <xs:element name="GPSStatus" type="xs:anyType" minOccurs="0"/>

      <xs:element ref="pdal:LESStatus" minOccurs="0" maxOccurs="unbounded"/>

    </xs:sequence>

    </xs:complexType>

    </xs:element>

    </xs:schema>

    GPSStatus.xsd:

    <?xml version="1.0" encoding="UTF-8"?>

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pdal="http://mycompany.com/PDAL" targetNamespace="http://mycompany.com/PDAL" elementFormDefault="qualified" attributeFormDefault="unqualified">

                <xs:include schemaLocation="../CommonElementType.xsd"/>

                <xs:include schemaLocation="../DeviceStatus.xsd"/>

                <xs:include schemaLocation="DAGRCommonType.xsd"/>

                <xs:element name="GPSStatus">

                            <xs:complexType>

                                        <xs:complexContent>

                                                    <xs:extension base="pdal:DeviceStatusType">

                                                                <xs:sequence>

    <xs:element name="OperationalMode" type="pdal:GPSOpModeContentType">

                                                                            </xs:element>

                                                                </xs:sequence>

                                                    </xs:extension>

                                        </xs:complexContent>

                            </xs:complexType>

                            <xs:key name="keyGPSLinkStatus">

                                        <xs:annotation>

                                                    <xs:documentation>Specifies that the protocol of each link status as unique.</xs:documentation>

                                        </xs:annotation>

                                        <xs:selector xpath="pdal:LinkStatus"/>

                                        <xs:field xpath="@protocol"/>

                            </xs:key>

                </xs:element>

    </xs:schema>

    After assigning GPSStatus element with pdal:GPSStatus type object inVehicleStatus object and convert to XMLString:

    <?xml version="1.0" encoding="utf-8"?>

    <pdal:VehicleStatus version="1.0" xmlns:pdal="http://mycompany.com/PDAL">

        <pdal:GPSStatus d2p1:type="pdal:DAGRStatus" deviceID="U3A00190-DAGR" schemaVersion="1.0" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">

        <pdal:LinkStatus protocol="LNS">UP</pdal:LinkStatus>

          <pdal:OperationalMode>CONTINUOUS</pdal:OperationalMode>

       </pdal:GPSStatus>

       <pdal:LESStatus deviceID="LESV2-1" schemaVersion="1.0">

         <pdal:LinkStatus>UP</pdal:LinkStatus>

         <pdal:EthernetPortStatus port="1">Connected</pdal:EthernetPortStatus>

         <pdal:EthernetPortStatus port="2">Connected</pdal:EthernetPortStatus>

       </pdal:LESStatus>

    </pdal:PlatformStatus>

    I use the following code to validate the PlatformStatus.xml against the xmlSchemaSet which contains both VehicleStatus.sxd and GPSStatus.xsd

    XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();

    // Load primary VehicleStatus schema

    XmlTextReader textReader = new XmlTextReader("c:/VehicleStatus.xsd");

    xmlSchemaSet.Add(null, textReader);

    //Load DAGRStatus schema// Load primary GPSStatus schema

    textReader = new XmlTextReader("c:/GPSStatus.xsd");

    xmlSchemaSet.Add(null, textReader);

    When validate and get the following:

    This is an invalid xsi:type ‘http://mycompany.com/PDAL:GPSStatus’

    By GPSStatus schema is included in the xml schema set object already event though VehicleStatus.xsd does not include GPSStatus.xsd because it does not needed due to GPSStatus is anyType in VehicleStatus schema.

    Any idea?

    Thanks

    Friday, April 27, 2012 3:59 PM