Search code examples
xmlxsdxsd-validationxsd-1.1

Extend XSD schema to allow a new attribute in one XSD standard element type


I need to add different types of documentation inside a XSD file. I would like be able to add an attribute named "type" to the documentation elements like here:

<xs:annotation>

   <xs:documentation type="doc">
       My documentation....
   </xs:documentation>

   <xs:documentation type="example">
       My first example...
   </xs:documentation>

   <xs:documentation type="example">
       My second example...
   </xs:documentation>

   <xs:documentation type="tip">
        My tip.
   </xs:documentation>

</xs:annotation>

I can change all needed in the XSD to make this possible. How can I do that?


Solution

  • The schema for schema already allows that, but only for attributes in a custom namespace so e.g.

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:my="http://example.com/my-annotation-types"
        xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified"
        vc:minVersion="1.1">
        
        <xs:element name="foo" type="xs:string">
            <xs:annotation>
                <xs:documentation my:type="doc"></xs:documentation>
            </xs:annotation>
        </xs:element>
        
    </xs:schema>
    

    is allowed as the schema for schema declares and uses and extends xs:annotated in most places:

    <xs:complexType name="openAttrs">
        <xs:annotation>
          <xs:documentation>
           This type is extended by almost all schema types
           to allow attributes from other namespaces to be
           added to user schemas.
         </xs:documentation>
        </xs:annotation>
        <xs:complexContent>
          <xs:restriction base="xs:anyType">
            <xs:anyAttribute namespace="##other" processContents="lax"/>
          </xs:restriction>
        </xs:complexContent>
      </xs:complexType>
      <xs:complexType name="annotated">
        <xs:annotation>
          <xs:documentation>
           This type is extended by all types which allow annotation
           other than &lt;schema> itself
         </xs:documentation>
        </xs:annotation>
        <xs:complexContent>
          <xs:extension base="xs:openAttrs">
            <xs:sequence>
              <xs:element ref="xs:annotation" minOccurs="0"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID"/>
          </xs:extension>
        </xs:complexContent>
      </xs:complexType>