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

Time:11-12

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?

CodePudding user response:

In addition to @MartinHonnen's idea of using a namespace on the type attribute, here are some other possibilities:

  1. Use a child type element:

    <xs:documentation>
      <type>tip</type>
      My tip.
    </xs:documentation>
    
  2. Use a wrapping element named per type, generally a preferable practice than using type attributes anyway:

    <xs:documentation>
      <tip>
        My tip.
      <tip>
    </xs:documentation>
    

These added elements can themselves be in a namespace of your own design of course.

CodePudding user response:

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>
  • Related