开发者

JAXB IDREF and ID usage?

开发者 https://www.devze.com 2022-12-20 03:52 出处:网络
I\'m looking to write an XSD which will be used to generate some Java classes via JAXB. I\'d like the resulting XML to look like this:

I'm looking to write an XSD which will be used to generate some Java classes via JAXB. I'd like the resulting XML to look like this:

<Appointment>
    <Patient ref="12345">Bob Smith</Patient>
    <Type>Some Appointment Type</Type>
    <Date>2010-02-17</Date>
    ....
</Appointment>

So, given this schema I'm wanting it to generate a class where I can just do something like this:

Patient p = loadPatientFromDB();
Appointment a = new Appointment();
a.setPatient(p);
a.setType("Some Appointment Type");

I think what I am looking to do involves having an element which has an IDREF as an attribute and then a string as the contents of the element.

Can someone give me a hand with some of the XSD?

Thanks!

EDIT

This question could likely also be asked i开发者_StackOverflow中文版n the following manner.

Can a simpleType have an attribute, or must it be a complexType.

So, can you have

  <element id="foo">bar</element>

or must you have

  <element id="foo"><name>bar</name></element>


I will answer your second question, since it's very clearly asked. No, a simple type cannot have an attribute. What you are looking for is a complex type with simple content:

<xs:complexType name="Person">
    <xs:simpleContent>
        <xs:extension base="xs:string">
           <xs:attribute name="ref" type="xs:IDREF" use="required"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

Edit: You can now use this type as you intended, assign it to an element called "person" and you can have <person id="foo">A Name</person>.

As for the first question: you still have a bit of extra work if you actually want the IDs resolved. For example, see here.

0

精彩评论

暂无评论...
验证码 换一张
取 消