This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes XML Schema - type vs. ref Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML Schema - type vs. ref" Watch "XML Schema - type vs. ref" New topic
Author

XML Schema - type vs. ref

S Larkham
Greenhorn

Joined: Mar 14, 2006
Posts: 8
A beginner's question - what are the respective advantages of declaring an element using a defined type, as opposed to using "ref" to pull in a definition?
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
With type attribute you can reuse the entire type declaration. For example many element will use the same type of content like

<element name="a">
<complexType>
<sequence>
<element name="title" type="string"/>
<element name="subject" type="string"/>
<element name="price" type="string"/>
<element name="isbn" type="string"/>
</sequence>
</complexType>
</element>


<element name="b">
<complexType>
<sequence>
<element name="title" type="string"/>
<element name="subject" type="string"/>
<element name="price" type="string"/>
<element name="isbn" type="string"/>
</sequence>
</complexType>
</element>

Instead of declaring local copies like this we can create a global type declaration like

<complexType name="author">
<sequence>
<element name="title" type="string"/>
<element name="subject" type="string"/>
<element name="price" type="string"/>
<element name="isbn" type="string"/>
</sequence>
</complexType>

and refer to it like

<element name="a" type="prefix:author"/>

Now lets see "ref" attribute

<element name="firstName" type="string"/>
<element name="middleName" type="string"/>
<element name="lastName" type="string"/>
<element name="nickName" type="string"/>

<element name="name">
<complexType>
<sequence>
<element ref="prefix:firstName"/>
<element ref="prefix:lastName"/>
</sequence>
</element>

Look at the above declaration. We don't need all those declarations for our name element. So we used "ref" attribute to refer to ONLY those that are required for our name element declaration.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
S Larkham
Greenhorn

Joined: Mar 14, 2006
Posts: 8
Cheers for that.
But what if, instead of just using the "ref" attribute to refer to one of the children of the "name" attribute, you had defined "name" as an element rather than a type, then used it in the declaration of another element, e.g.
<element name="customerName" ref="name"/>
(i.e. similarly to the way you'd use "type") - are there advantages of one way over another?
This may seem a rather silly question - but an XML guru at our place encouraged us to use "ref" rather than "type" for this sort of thing; and we all wonder why? (he's gone on to pastures new, so we can't ask him!)
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

But what if, instead of just using the "ref" attribute to refer to one of the children of the "name" attribute, you had defined "name" as an element rather than a type, then used it in the declaration of another element, e.g.
<element name="customerName" ref="name"/>


I didn't quite get you here.

<element name="customerName" ref="name"/>

The above declaration is syntatically wrong.

You cannot have both name and ref attribute together. You either declare the element as a global element and refer to it with in your content model OR declare a local element with a name attribute.
S Larkham
Greenhorn

Joined: Mar 14, 2006
Posts: 8
Oops - deep embarrassment - it had been a long day!
Let me rephrase........
What I meant was this: What are the advantages or otherwise of defining a type within the schema, rather than "ref"ing, say, an externally-defined element? (aside from the obvious advantage that it can be easily re-used by others)
S Larkham
Greenhorn

Joined: Mar 14, 2006
Posts: 8
Or (as a colleague of mine pointed out), if both the "ref" and the "type" are referring to something defined in an external file, what are the advantages and disadvantages of using one rather than the other?
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
I've always used type, but maybe that is because I'm mostly defining complexTypes and only the root node is actually an element (pretty much a necessity with JAXB unless you want it to generate twice as many Java interface files). Out of curiosity I did some digging and I think I figured out the issue. I don't think it is an issue of "ref" versus "type", so much as a case of using each where they get you want you want. The distinction might be a bit easier with an example that doesn't use XmlSchema, but uses DTD instead.



If we were using DTDs instead of XmlSchema, the former example would be the result of us typing in two element definitions that, except for element name, looked identical:



The second example shown would have a DTD like:



The first example is a case of using "type". At any level of the grammar you are saying that the "type" tells you about the children one level down.

The second example is a case of using "ref". With ref you've ALREADY dropped one level down and you are using the "ref" to describe the CURRENT node, not its children. You could use "ref" to also construct the equivalent of the first DTD, but you'd be working at a different level of the tree; you would use "ref" to bring in the Price and Currency individually, instead of using "type" to bring them both in as part of a consistently-used type structure.

The distinction tends to get fuzzy because when you use "type" you are simultaneously specifying the name of the node you are defining AND using the type to declare (by re-use) the content model for the node. With "ref" you are yanking in the node directly, and that node already has its own name and its own content model. When dealing with a single node it isn't a big deal one way or the other, but when dealing with multiple nodes "type" provides more enforcement of the grammar for allowable child combinations than "ref" does for that exact same level of the tree.

If you tend to mostly define types and create very few elements than I think you'll mostly using the "type" attribute during composition of types. A good test (at least for me) is to see what kind of code JAXB generates for your schema. If you get a Java representation that you think accurately reflects your intended content model then you are ok, if not you have to look at how you are using element and type definitions.
[ March 19, 2006: Message edited by: Reid M. Pinchback ]

Reid - SCJP2 (April 2002)
S Larkham
Greenhorn

Joined: Mar 14, 2006
Posts: 8
Thanks very much - that sounds like what we were looking for.
Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188



The above line is mentioned in one of the code listings of Professional XML 2nd Edition.

Is this correct ?

From what i understand ref & type cannot be used together.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML Schema - type vs. ref
 
Similar Threads
reference
Lookup problem for local beans on oc4j
cmp 2.x jndi local lookup fails
Lookup EJB on Different Application servers
unresolved ejb-link