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?)
Definition and Usage The extension element extends an existing simpleType or complexType element.
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.
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.
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.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With