Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Elements in XSD Complex Type Without New Element

I have this complex type:

<xsd:complexType name="Identifier">
    <xsd:sequence>
        <xsd:element name="Id" type="xsd:string"/>
        <xsd:element name="Version" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

Now I want to include this in another complex type and I've been doing that like this:

<xsd:complexType>
    <xsd:sequence>
        <xsd:element name="Id" type="Identifier"/>
              <!-- More elements here -->
    </xsd:sequence>
</xsd:complexType>

This isn't what I really want though. I want to include the Identifier type's elements directly in my second complex type without creating a new element. E.g. the same as just doing this:

<xsd:complexType>
    <xsd:sequence>
        <xsd:element name="Id" type="xsd:string"/>
        <xsd:element name="Version" type="xsd:string"/>
              <!-- More elements here -->
    </xsd:sequence>
</xsd:complexType>

Hope that makes sense.

Thanks in advance.

like image 492
ng5000 Avatar asked Oct 16 '09 12:10

ng5000


People also ask

How do you refer to complex type in xsd?

Define a Complex Type directly by naming. S.No. Complex Empty complex type element can only have attributes but no contents. Text-Only complex type element can only contain attribute and text. Mixed complex type element can contain element, attribute and text.

What does complexType mean in xsd?

The complexType element defines a complex type. A complex type element is an XML element that contains other elements and/or attributes.


2 Answers

You can extend types, like this:

<xsd:complexType name="MySubType">
    <xsd:complexContent>
        <xsd:extension base="Identifier">
                       <xsd:sequence>
                            <!-- More elements here -->
                       </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
like image 122
skaffman Avatar answered Sep 28 '22 01:09

skaffman


this complex type will always resolve to

<Identifier>
   <Id>string</Id>
   <Version>string</Version>
</Identifier>

if you don't want a child structure, you could define Id and Version as elements and reference them using

<xsd:element ref="Id"/>
<xsd:element ref="Version"/>

later on. But then you don't have the guarantee that they both occur

You can also make Id and Version attributes to the Identifier element in a complex type

good luck Mike

like image 33
MikeD Avatar answered Sep 28 '22 02:09

MikeD