views:

414

answers:

1

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

A: 

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" />
Ben
Wouldn't this force me to encapsulate the element every time I called it? <element name="bar" type="fooType"> would result in syntax like <bar><foo /></bar> rather than simply <bar /> ... which is undesirable.
Mark E
No, the result is I think what you were asking for: <bar><fooElement/><barElement/></bar>
Ben
Given your edit I'm inclined to agree -- thanks.
Mark E