aspose file tools
The moose likes XML and Related Technologies and the fly likes newbie question: ref or complex type ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "newbie question: ref or complex type ?" Watch "newbie question: ref or complex type ?" New topic
Author

newbie question: ref or complex type ?

Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 310
I have an XML document, with one structure nested inside another - e.g "manager" element nested inside a "company":


I'm trying to create a schema (xsd) for this.
Note my "manager" needs to be defined outside the scope of "company", so that it's re-usable (i.e. manager may be nested inside other structures, not just "company").
I get the impression there are 2 ways to do it:
1. Define "manager" as *element*, then let "company" declare a *ref* to it


2. Define "manager" as *complex type*


Could anyone please tell what's the difference between the 2 approaches ? Why does XML define 2 syntaxes to achieve the same goal ?
Thanks
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
This feature provides an easy way of declaring element-types that are used multiple times in the Schema. You have only one global declaration and use the ref attribute in other local element declarations to reference the definition of the global element.

Say you want to associate an address to your XML example. The company has an address of its own. The manager may have multiple addresses - work and home and as such each employee of the company has another address, which could be different. To acheive this, you declare a global element called address with a type and then use the ref attribute to refer to that type.
Hope this helps...

- m


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 310
Thanks very much for your reply.
It's just that I thought that the same thing (reusable elements) can be achieved with "complexType" ... when would you use "complexType", and when would you prefer "ref" to an element ?

Thanks again.
Hari Vignesh Padmanaban
Ranch Hand

Joined: Oct 07, 2003
Posts: 578
"In general, the value of the ref attribute must reference a global element, i.e. one that has been declared under schema rather than as part of a complex type definition"

So




is not possible
[ August 04, 2005: Message edited by: Hari Vignesh Padmanaban ]
Hari Vignesh Padmanaban
Ranch Hand

Joined: Oct 07, 2003
Posts: 578
Originally posted by Sol Mam-Orn:
Thanks very much for your reply.
It's just that I thought that the same thing (reusable elements) can be achieved with "complexType" ... when would you use "complexType", and when would you prefer "ref" to an element ?

Thanks again.


Well complex typoes have a lot of features that go along with them

  • It is possible to achieve "inheritance" through defining complex types using the <xs:extension> attribute
  • It is possible to choose (or restrict) definitions in a complex type using the <xs:restriction> attribute
  • You can make a complex type abstrat using the abstract attribute
  • You can also make complex types final by using the final attribute

  • And you cant do any of this with <xs:element>

    Hope that helps

    [ August 04, 2005: Message edited by: Hari Vignesh Padmanaban ]

    Just to clear things up,

    When you use <complexType> inside teh definition of an element, rather than as a seprate definition, like


    you cant

  • Give it a name
  • Use the final or abstract attributes
  • Resue it across several schemas

  • hth
    [ August 04, 2005: Message edited by: Hari Vignesh Padmanaban ]
    Madhav Lakkapragada
    Ranch Hand

    Joined: Jun 03, 2000
    Posts: 5040
    Originally posted by Hari Vignesh Padmanaban:
    "In general, the value of the ref attribute must reference a global element, i.e. one that has been declared under schema rather than as part of a complex type definition"

    So




    is not possible



    Well, this is NOT possible under the assumed condition that the parent of the complexType is NOT the schema element ie; the complexType named managerType is NOT a global definition.

    Or am I way off......

    - m
    Hari Vignesh Padmanaban
    Ranch Hand

    Joined: Oct 07, 2003
    Posts: 578


    As you can see, is declared under teh schema element If you change to the parser will still complain ( I am using XmlSpy IDE)

    but the following lines


    in the works !!!

    and changing to does not work as expected !!



    hmm seems like more investigation has to be done ..meanwhile any XML experts are welcome to suggest your ideas

    And the lines ""In general, the value of the ref attribute must reference a global element, i.e. one that has been declared under schema rather than as part of a complex type definition" were quoted from W3C specs !!

    mabe teh last lines should be "one that has been declared under schema rather than as part of a complex type definition or not a complex type definition"


    HTH
    [ August 06, 2005: Message edited by: Hari Vignesh Padmanaban ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: newbie question: ref or complex type ?
     
    Similar Threads
    Inhertiting complex type in XML Schema
    Doubt on XML Schema validity
    XMLSchema - Global Attribute Declaration
    regarding xml syntax
    XML Schema: Use of Import