0

I am trying to create an XSD to validate an element like this:

<productColor Colortype="HEX">353535</productColor>

so, I tough by my self it is so easy, I do something like this:

    <xs:element name="productColor" type="xs:string">
        <xs:complexType>
            <xs:attribute name="Colortype" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="HEX"/>
                        <xs:enumeration value="RGB"/>
                        <xs:enumeration value="sRGB"/>
                        <xs:enumeration value="CMYK"/>
                        <xs:enumeration value="HSV"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>

But, but when I've done some search I found out some body (like this link in W3 ) defined attribute but using extension , like this code:

<xs:element name="productColor">
<xs:complexType>
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="Colortype" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="HEX"/>
                        <xs:enumeration value="RGB"/>
                        <xs:enumeration value="sRGB"/>
                        <xs:enumeration value="CMYK"/>
                        <xs:enumeration value="HSV"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

Why? It is wrong if we go in my way?(don't use extension) and I found this link from Microsofit which says you can do it without extension. So now, I am totally confused.

Ilona
  • 357
  • 3
  • 10
mil
  • 137
  • 7
  • see [XML Schema: Element with attributes containing only text?](http://stackoverflow.com/questions/376582/xml-schema-element-with-attributes-containing-only-text) – Kachna Dec 03 '15 at 11:25
  • @Kachna i saw this, but what about the Microsoft link? – mil Dec 03 '15 at 11:41
  • the Microsoft link does not say : you can do it without extension. – Kachna Dec 03 '15 at 11:59
  • @Kachna but in the link, they have an example without extension. – mil Dec 03 '15 at 12:23
  • If you talk about ``. the quantity element is an element with text-only and doesn't have an attribute. – Kachna Dec 03 '15 at 12:47
  • I think you know that a complex type element is an XML element that contains **other elements and/or attributes**. – Kachna Dec 03 '15 at 12:56
  • @Kachna yes, yes, i know it. and no i am talking about this: ``. OrderInfo is an element witch contain 2 other element and have an attribute, so basically OrderInfo is an complex element. i am wounder why they didn't use extension to add attribute, as w3 say. – mil Dec 03 '15 at 13:20
  • see [this](http://www.xml.com/pub/a/2001/08/22/easyschema.html) – Kachna Dec 03 '15 at 16:15

1 Answers1

0

A data types can be either named (i.e., global) or anonymous (i.e., local). Global definitions must have a name and be a top-level element that is included directly in the xs:schema document element. Local types are defined directly where they are needed in a schema; they are anonymous (i.e., no name attribute); and they have a local scope.

In your case, the productColor element has two types (which is wrong) :

  1. the xs:string predefined simple datatype.

  2. the local complex type (anonymous).

Complex types with simple content models are created by adding a list of attributes to a simple type. The operation of adding attributes to a simple type to create a simple content complex type is called an extension of the simple type. see xs:simpleContent Element

Kachna
  • 2,921
  • 2
  • 20
  • 34