• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

newbie question: ref or complex type ?

 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 578
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 578
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Posts: 5040
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 578
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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 ]
     
    My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic