aspose file tools*
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes Confused in XML Schema. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "Confused in XML Schema." Watch "Confused in XML Schema." New topic
Author

Confused in XML Schema.

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
From the RMH Book, Page 34. A Sample Schema


A XML based on it:



Confused on some things-
1. the prefix mh is declared but not used, neither in the schema nor in the XML instance.
2. Now the addr is seen as a newly added prefix in the XML instance. Where did it come from suddenly?
3. What is the purpose of this line in the schema:
<element name="address" type="mh:USAddress" />

Why to use the prefix mh if not used anywhere? If we were to use it in the schema/the XML instance, whot would be the change?

Please expain,
[ April 18, 2008: Message edited by: Niranjan Deshpande ]

SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Niranjan: the prefix mh is declared but not used, neither in the schema nor in the XML instance.

It is being used as below, in the schema:
<element name="address" type="mh:USAddress" />

Niranjan: Now the addr is seen as a newly added prefix in the XML instance. Where did it come from suddenly?

It is the same namespace as mh defined in the schema. Namespace prefix are arbitrary. It is not a compulsion that the same prefix must be used both in the schema and the instance. As you can see in your example "addr" in the instance and "mh" in the schema both refer to the same url: http://www.Monson-Haefel.com/jwsbook. As long as the prefixes point to the same url they are considered the same. Prefix is just a convenience mechanism to avoid things like these:
<http://www.Monson-Haefel.com/jwsbook:address>
The above holds the same meaning as <addr:address xmlns:addr="http://www.Monson-Haefel.com/jwsbook">
So, you can say that namespace prefix is just an alias/nickname/shortname to refer to the namespace url.
Niranjan:What is the purpose of this line in the schema:
<element name="address" type="mh:USAddress" />

Types i.e. complexType, simpleType, etc. are analogies to class definitions in java and elements are like object instances. So, in the above line, it is saying that I have an element named "address" that is of the type USAddress defined elsewhere under the namespace "http://www.Monson-Haefel.com/jwsbook". This is a nice article to understand namespaces.
[ April 18, 2008: Message edited by: Nitesh Kant ]

apigee, a better way to API!
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Thanks.
It's bit clear now. But I think it will take time to become really comfortable with
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Nitesh Kant:
<http://www.Monson-Haefel.com/jwsbook:address>


A convention that is becoming increasingly common for representing fully qualified names with the full namespace in prose is:

{http://www.Monson-Haefel.com/jwsbook}address

see James Clark: XML Namespaces

Ronald Bourret's XML Namespaces FAQ
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Peer, many thanks for that value addition.

But I think I will get comfortable with the basics first, and then go for such add ons.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Schema doubts, contd: Some excerpts from the RMH:

Listing 3.15



Listing 3.18



The book says: Listing 3-15 requires that all elements be qualified. Listing 3-18 declares the default namespace, which is the namespace automatically assigned any element that is not prefixed, so even though the elements in Listing 3-18 are not prefixed, they are qualified and are therefore valid when checked against the XML schema in Listing 3-15.

This just made me go . Please explain
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5

demands that the parent and child elements use the namespace prefix. The default is "unqualified" which means that child elements are automatically assumed to be in the namespace of the parent so that they wouldn't have to have a prefix.


would be valid for elementFormDefault="unqualified" but is invalid for elementFormDefault="qualified".



this is valid for elementFormDefault="qualified". It is clear that "mh:accountName" is "{http://www.Monson-Haefel.com/jwsbook}accountName"

However if you now make the namespace the default namespace then

is valid for elementFormDefault="qualified" because {http://www.Monson-Haefel.com/jwsbook} is the default namespace which means that "accountName" is actually "{http://www.Monson-Haefel.com/jwsbook}accountName"

This gives rise to the following confusing situation: When an element doesn't have a prefix:
  • then it may not belong to any namespace as long as none of its parents belong to any namespace.
  • then it may belong to the namespace of the closest parent with a namespace
  • it may belong to the closest default namespace.


  • So if you have an element without a prefix with a parent that declares a default namespace that is different form its own - what namespace does that element belong to?
    This is the reason why the basic profile demands elementFormDefault="qualified".


    Anyway you may want to have a look at this topic and this topic before your head caves in.
    [ April 18, 2008: Message edited by: Peer Reynders ]
    Niranjan Deshpande
    Ranch Hand

    Joined: Oct 16, 2005
    Posts: 1277
    Peer, I am getting things. Finally making sense. Thanks a lot.

    What sounded crap a couple of days back has suddenly unraveled like a mystery

    So

    So if you have an element without a prefix with a parent that declares a default namespace that is different form its own - what namespace does that element belong to?


    Keeping in mind listings 3.15 nd 3.18, answer to this is:then it belongs to the namespace of the closest parent with a namespace (3rd bullet point above)

    Further question: Are we referring nested parents when we say closest parent?
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    This is the situation I was alluding to:


    Are we dealing with {http://www.foo.org/}B or {http://www.bar.org/}B ?




    Here we are dealing with {http://www.bar.org/}B because the lack of a prefix indicates the use of the default namespace.


    Basic Profile 1.0 - 4.1.13

    4.1.13 SOAP Body and Namespaces
    The use of unqualified element names may cause naming conflicts, therefore qualified names must be used for the children of soap:Body.

    R1014 The children of the soap:Body element in a MESSAGE MUST be namespace qualified.

    [ April 21, 2008: Message edited by: Peer Reynders ]
    Niranjan Deshpande
    Ranch Hand

    Joined: Oct 16, 2005
    Posts: 1277
    Hi Peer,

    The specific case that you told is something different than what I was thinking of. But still, I am getting a hang of this now.

    By the way, the section 3.2.3.2. Union types in RMH is conceptually clear. But would like to read more code and some simple explanations. Anywhere available?

    Also, a query on anonymous types: My understanding of anonymous types is:

    Since these types are local to the schema element, we cannot reuse them in XML instances as independent stand alone XML chunks.

    correct?
    [ April 21, 2008: Message edited by: Niranjan Deshpande ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Confused in XML Schema.