Use XML Schema to extend an element rather than a complexType

xml, xsd

Solution

I would create a new type that mimics foo element and then extend from it. While it's not the best solution, you do get the same result as if you were able to extend the element directly.

  <xs:complexType name="fooType">
    <xs:sequence>
      <xs:element name="fooElement" type="xs:string" />
    </xs:sequence>
   </xs:complexType>

   <xs:complexType name="barType">
    <xs:complexContent mixed="false">
      <xs:extension base="fooType">
        <xs:sequence>
          <xs:element name="barElement" type="xs:string" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:element name="bar" type="barType" />

Problem

Suppose I have some schema: ``` <xsd:schema ...> <xsd:element name="foo"> <xsd:complexType> <xsd:sequence> <xsd:element name="fooElement" type="xs:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> ``` This defines some element `foo` which has inside it some element `fooElement` with type string. I'd like to now extend element `foo` to also have an element `barElement` and call this extension `bar`. To complicate things, let's also suppose that someone else has defined `foo` and the schema cannot be changed. While the example here is trivial, let's also assume that the contents of `foo` might be more complicated, and that defining a new schema isn't quite as simple as copying the element `fooElement`. Effectively, I'd like to define a new schema: ``` <xsd:schema xmlns:fns="otherschema" ...> <xsd:import namespace="otherschema" /> <xsd:element name="bar"> <xsd:complexContent> <xsd:extension base="fns:foo"> <xsd:sequence> <xsd:element name="barElement" type="xs:string" /> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:element> </xsd:schema> ``` Unfortunately `<xsd:extension>`'s `base` attribute only accepts XSD type arguments, not elements. How do I extend an element? (can I extend an element?)

Original source