Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use XML Schema to extend an element rather than a complexType

Tags:

xml

xsd

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?)

like image 986
Mark Elliot Avatar asked Sep 30 '09 18:09

Mark Elliot


People also ask

What is extension in XML Schema?

Definition and Usage The extension element extends an existing simpleType or complexType element.

What is simple type and complexType in XSD?

XSD Elements. XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is complexType?

A complex type is essentially a type definition for elements that may contain attributes and elements. An element can be declared with a type attribute that refers to a complexType element that defines the structure, content, and attributes of that element.

What is minOccurs and maxOccurs in XML?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.


1 Answers

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" />
like image 181
Ben Avatar answered Oct 13 '22 01:10

Ben