• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

override an attribute from parent element?

 
Jack Wanes
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there:

I have two elements with corresponding complex types, student and grad_student. grad_student extends student which has an required attribute called "salary". Now if I I want to make "salary" attribute "optional" in for grad_student, how can I do that?

<xsd:complexType name="student">
<xsd:complexContent>
<xsd:extension base="xyz:person">
<xsd:attribute name="sid" type="xsd:ID" use="required"/>
<xsd:attribute name="salary" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="student" type="xyz:student"/>
<xsd:complexType name="grad_student">
<xsd:complexContent>
<xsd:extension base="xyz:student">
<xsd:attribute name="s_name" type="xsd:string" use="optional"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="grad_student" type="xyz:grad_student"/>



The following fails since salary is inherited and cannot be declared in the
child element again:

<xsd:complexType name="student">
<xsd:complexContent>
<xsd:extension base="xyz:person">
<xsd:attribute name="sid" type="xsd:ID" use="required"/>
<xsd:attribute name="salary" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="student" type="xyz:student"/>
<xsd:complexType name="grad_student">
<xsd:complexContent>
<xsd:extension base="xyz:student">
<xsd:attribute name="s_name" type="xsd:string" use="optional"/>
<xsd:attribute name="salary" type="xsd:string" use="optional"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="grad_student" type="xyz:grad_student"/>



regards,
 
Srivatsa Katta
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jack,

Am not sure if you can override an attribute like that, but your requirement can be achieved in almost a similar way as yours but with an additional type.

Find the below given sample XSD which suits your requirement :



Sample XML for the above XSD :



In the xml you can observe the element grad_student, the first occurrence has the salary attribute where as the second doesn't.

Regards,
Srivatsa Katta
 
Jack Wanes
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that may be the only way to solve this problem as I thought?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic