How to reference an element in and XSD and give it a new name?

2.4k views Asked by At

I need to re use a status tag that I created in an XSD. For example in our order company we have several statuses. Status of an order- car order, truck order etc.

The tags that need to be used are unfortunately not the same. For the car it is a carStatus and for the truck it is a truckStatus but the underlying object are the same. It is a xs:string tag that has an enumeration of COMPLETED, BUSY or AWAITING INFORMATION.

Now I don not want to have 16 tags for 16 objects (car, track, chopper... -Status). Tomorrow if we add another status I have to go to all of these elements and update it.

My XSD where I reference the GenericCodeStatus looks as follows

<xs:schema targetNamespace="http://www.myDomain.co.za/myCoreXsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mine="http://www.myDomain.co.za/myCoreXsd" elementFormDefault="qualified" attributeFormDefault="unqualified">
      <!-- Defining my Enum -->
<xs:element name="GenericCodeStatus">
    <xs:annotation>
        <xs:documentation>Generic code status</xs:documentation>
    </xs:annotation>
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="COMPLETED"/>
            <xs:enumeration value="BUSY"/>
            <xs:enumeration value="AWAITING INFORMATION"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

      <!-- Here I have mytag where I am referencing the genericCodeStatus -->
<xs:complexType name="MyTag1">
    <xs:sequence>
        <xs:element ref="mine:GenericCodeStatus"/>
    </xs:sequence>
</xs:complexType>

Now the thing is that I want to have the genericCodeStatus under MyTag1 to have a name. I tried creating it with a name and type tag (and i am using XML Spy as an editor)

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.myDomain.co.za/myCoreXsd"    xmlns:mine="http://www.myDomain.co.za/myCoreXsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified"  attributeFormDefault="unqualified">
<!-- Defining my Enum -->
<xs:element name="GenericCodeStatus">
    <xs:annotation>
        <xs:documentation>Generic code status</xs:documentation>
    </xs:annotation>
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="COMPLETED"/>
            <xs:enumeration value="BUSY"/>
            <xs:enumeration value="AWAITING INFORMATION"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>
<xs:complexType name="MyTag1">
    <xs:sequence>
        <!-- Taken this out -->
        <xs:element ref="mine:GenericCodeStatus"/>
        <!-- and replace it with name and type -->
        <xs:element name="carStatus" type="mine:GenericCodeStatus"/>
    </xs:sequence>
</xs:complexType>

But then get an error of an undefined value for 'type' encountered error. I have also tried removing the 'mine' namespace.

If I try to replace the type with a ref as in

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.myDomain.co.za/myCoreXsd" xmlns:mine="http://www.myDomain.co.za/myCoreXsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- Defining my Enum -->
<xs:element name="GenericCodeStatus">
    <xs:annotation>
        <xs:documentation>Generic code status</xs:documentation>
    </xs:annotation>
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="COMPLETED"/>
            <xs:enumeration value="BUSY"/>
            <xs:enumeration value="AWAITING INFORMATION"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>
<xs:complexType name="MyTag1">
    <xs:sequence>
        <!-- Taken this out -->
        <xs:element ref="mine:GenericCodeStatus"/>
        <!-- and replace it with name and ref -->
        <xs:element name="carStatus" ref="GenericCodeStatus"/>
    </xs:sequence>
</xs:complexType>

The validation works fine but if I save it XML Spy is removing the name element and I am back to where I started.

If anyone knows please?

1

There are 1 answers

0
cp5 On BEST ANSWER

I found the answer to this but thought of still posting this as it was difficult to find. Thanks to a buddy of mine :)

I should not define the Element like I did and then try to reference it over and over each time with a new name.

What I should do is to define the simpleType on its own and give it a name.

<!-- Define the simpleType as an enum and give it a name -->
<xs:simpleType name="myCoolDataType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="COMPLETED"/>
        <xs:enumeration value="BUSY"/>
        <xs:enumeration value="AWAITING INFORMATION"/>
    </xs:restriction>
</xs:simpleType>

Then where I want to use it I need to define the element and just give it a type with the value you gave to the enum in the first stip

<xs:complexType name="MyTag1">
    <xs:sequence>
        <xs:element name="truckStatus" type="mine:myCoolDataType"/>
        <xs:element name="carStatus" type="mine:myCoolDataType"/>
    </xs:sequence>
</xs:complexType>