File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes override an attribute from parent element? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "override an attribute from parent element?" Watch "override an attribute from parent element?" New topic
Author

override an attribute from parent element?

Jack Wanes
Greenhorn

Joined: Sep 18, 2006
Posts: 22
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

Joined: Aug 13, 2005
Posts: 55
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

Joined: Sep 18, 2006
Posts: 22
Thanks, that may be the only way to solve this problem as I thought?
 
Consider Paul's rocket mass heater.
 
subject: override an attribute from parent element?