简体   繁体   中英

XML Element Reference a Sibling

I am looking to have an element reference a sibling but I am unsure how to implement this in my schema. I have the following in my .xml file:

 <staff pid="818789">
     <supervisor staff="201022"></supervisor>
     ...
 </staff>

Meaning each member of staff will have a supervisor who is also a member of staff so I am looking to reference them if this is possible.

"pid" is a type I have created in my schema which is just a string of 6 digits.

I have the following in my .xsd file:

 <xs:element name="staff">
    <xs:complexType>    
        <xs:sequence>
            <xs:element name="supervisorID" type="pidType" minOccurs="0" maxOccurs="1"/>
            ...
        </xs:sequence>
        <xs:attribute name="pid" type="pidType" use="required"/>
    </xs:complexType>
 </xs:element>

It gives the following error:

ERROR - cvc-type.3.1.1: Element 'supervisor' is a simple type, so it cannot have attributes, excepting those whose namespace name is identical to ' http://www.w3.org/2001/XMLSchema-instance ' and whose [local name] is one of 'type', 'nil', 'schemaLocation' or 'noNamespaceSchemaLocation'. However, the attribute, 'staff' was found.

Which I can see why it raises the error but don't understand what I would change it to.

the element that has attributes must be complex type. So the supervisor element should be defined as follow

<xs:element name="supervisor">
        <xs:complexType>

                  <xs:attribute  name="staff" type = "pidType" use="required"/>

        </xs:complexType>

</xs:element>

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