guerda
guerda

Reputation: 24049

How can I describe a reference in a node to another one?

I have an XML format I need to parse. An example file is listed below:

<?xml version="1.0" encoding="UTF-8">
<root>
  <parent name="example1">
   <childdef ref="1" type="a" />
   <childdef ref="3" type="c" />
  </parent>

  <parent name="example2">
   <childdef ref="1" type="b" />
   <childdef ref="2" type="b" />
  </parent>

  <child id="1" name="Child 1" />
  <child id="2" name="Child 2" />
  <child id="3" name="Child 3" />
  <child id="4" name="Child 4" />
</root>

The parent elements contain at least one childdef element that refers to a child element. You must use a reference, because one child can be referenced by several parents (see child 1).

If I now use an XML mapper (e.g. a JAXB implementation), I would like to have a Object reference from Parent1 to Child 1. Now I only have the reference id as an attribute.

Is it possible to define this object reference in the XSD?

Upvotes: 4

Views: 4238

Answers (1)

musiKk
musiKk

Reputation: 15189

Yes, that's possible. See @XmlID and @XmlIDREF. They correspond to the XML types xsd:id and xsd:idref. But keep in mind that only strings can be IDs.

Upvotes: 5

Related Questions