tags:

views:

487

answers:

2

Hi,

I have a complexType like:

<xsd:complexType name="NightlyRate">
    <xsd:complexContent>
        <xsd:extension base="com:Money">
            <xsd:attribute name="night" type="com:Number" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

Now, I want to add a child element to "NightlyRate", which itself is of complexType. I tried adding:

<xsd:element name="xxx" type"com:Money"/>

after the complexContent element, but it throws error that element is not expected, also part of the problem is type of this complex-element is the same as type of extension base. I am using JAXB. Is there any other way to achieve this?

Thanks!

+2  A: 
<xsd:complexType name="NightlyRate">
    <xsd:complexContent>
        <xsd:extension base="com:Money">
            <xsd:sequence>
                <xsd:element name="xxx" type"com:Money">
                   <xsd:complexType>
                      <xsd:complexContent>
                             /// Attributes here
                      </xsd:complexContent>
                   </xsd:complexType>
                </xsd:element>
            </xsd:sequence>

            <xsd:attribute name="night" type="com:Number" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
womp
Some explanation here would probably be helpful.
Brabster
It throws following error:"The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'NightlyRate' is element only, but its base type is not."
Ankush
Ankush, that error means that your "xxx" element must also be a complexType. I assume that it is, and that you would have attributes for it. If it's not, and it's just a straight reference to another element type defined elsewhere, change it to <xsd:element ref="xxx" />, where xxx is the name of the element.
womp
I updated the code example to show the nested complex type.
womp
Hi Womp, Thanks for the code example n explanation, that made it so much clear. But, I still have a question...my "xxx" type is complexType, as I am saying its attribute type is com:Money, which itself is complexType in my schema. But, I don't really need to extend or add any attributes to it. So, shouldn't just saying the name of the element and its type suffice?, and if does, then why am I still getting this error?
Ankush
have you already declared <xsd:element name="xxx" /> somewhere else in your schema? if so, you can just use <xsd:element ref="xxx" /> instead of declaring a new element. if you haven't declared it somewhere else already, and it has no properties, consider making it an attribute of NightlyRate instead of an element.
womp
+1  A: 

To get sub-elements in a xsd:complexType, you have to put a xsd:sequence in it (first level, along with annotations and attributes), wich states that you want well... sub-elements. In this sequence, you can include elements of other defined types or define new types.

See XSD complex elements at W3Schools, and womp's example.

In some cases, you may want to use the xsd:any element to allow yet-undefined extensions (loose).

streetpc