aspose file tools*
The moose likes XML and Related Technologies and the fly likes Help needed on xmlns & targetNamespace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Help needed on xmlns & targetNamespace" Watch "Help needed on xmlns & targetNamespace" New topic
Author

Help needed on xmlns & targetNamespace

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi,

I'm new to XML and going through W3SCHOOLS.com tutorials.

Below is the listing
<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>
The following fragment:

xmlns:xs="http://www.w3.org/2001/XMLSchema"
indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:

This fragment:

targetNamespace="http://www.w3schools.com"
indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.


This fragment:

xmlns="http://www.w3schools.com"
indicates that the default namespace is "http://www.w3schools.com".

----------------------
It's written that "the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace" & it's also written that "targetNamespace="http://www.w3schools.com" indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace."

How come the elements come from two different links? Also what is the difference between these two and what is default namespace.?

Please advise.


Thanks,
Pramod
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 516
    
    3
[1]
How come the elements come from two different links? Also what is the difference between these two ...

Due to the inherent complication of the issue (language describing another language, or simply put meta-language), it is in fact not very easy to put the idea straight. This is the idea... take an "element" for illustration.

[1.1] <xs:element name="to" type="xs:string" />

However it is simple, it is already showing "elements come from two different links" as you put it.

[1.2] element xs:element : is called an element (as usually called in any xml related discourse).
[1.3] attribute name="to" : asserts the existence of an element named "to" of some type in the vocabulary of the target namespace.

That is about all it means of that part.

[2]
... and what is default namespace.?

This is a more subtle question as far as the schema validating engine is concerned. The bare meaning of it, I'm sure you know by looking at some xml documents, it means to say simply that there exists a specific namespace that, within the scope of its declaration, its elements can be referred to without the need of some arbitrary indentifying prefix. But that is far from the whole story in the arena of schema document.

[2.1] As far as the example of w3schools is concerned, its declaration, coincident with the target namespace, is of absolute no use. However, when anywhere within the schema itself involving some attribute referring to some top-level schema components that the schema is trying to described, its purpose would have a role to play.

I think the below would be helpful to illustrate the point.

[2.2] If I slightly modify the schema to use a reference (ref attribute) to a top-level schema component like this.

This is an equally good (though, not equivalent) schema for the same collection of xml documents. It is no longer a Russian doll model, and there is an additional top-level "element" called "body" that can be referred to at will. Now, the above makes use of the default namespace declaration, subtlely and quite implicitly.

[2.3] This is a transcription of it but this time without the default namespace declaration coincident with the target namespace, nevertheless there needs a namespace declaration all the same with prefix tns, say.

See the difference in the ref attribute?

[2.4] Since the original w3schools example does not involve any of these kinds of subtlety, the declaration of default namespace coincident with the target namespace is absolutely useless and it invites wild imagination by the un-informed. It can be taken out completely without changing any points it wants to make. I can produce some other variants than the [2.2] and [2.3] too. All in all, it can be quite involved, that's why I would say it is not very easy at the beginning. However, the line of reasoning though is consistent.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 516
    
    3
amendment
Sorry, I forgot to edit out type attribute in [2.2] and [2.3] line #13. The corresponding lines should read like below sucessively.

and
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi G Tsuji,

Thank you so much for the detailed explanation about my questions.

I felt that the information on W3Schools was a bit abstract so I googled it again.

What I've understood for the following code is that -:



A schema may contain two or more namespaces and one of them acts as a default namespace which doesn't need any prefix for it's call.

Here, Line No. 1 XMLSchema is used as a basic building block much like Java API which provides functionality for the content e.g Element, Attribute etc.
Line No. 2 defines another xml namespace which will be used.
Line No. 3 defines default namespace. If it contained anything like xmlns:john .... then prefix john needs to be used to refer to it.

Please correct me if I'm wrong for line 2 & 3.

Thank you.
>
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

I guess I messed up in the above example.



Here, Line 2 xmlns defines another namespace, while line 1 defines default namespace.

Please correct me.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 516
    
    3
[3]
A schema may contain two or more namespaces and one of them acts as a default namespace which doesn't need any prefix for it's call.

If certain way of saying can bring comfort, I would rewrite it like this. "A schema may contain one namespace only (http://www.w3.org/2001/XMLSchema itself) or more namespaces. None of them is required to be declared as a default namespace."
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"

Here, Line 2 xmlns defines another namespace, while line 1 defines default namespace.

Not at all. In no way, line 1 is intended to define a default namespace or prefixed namespace or anything of the sort. It defines the namespace (its uri) the schema document is to define (hence, targetted). Line 2 effectively defines a default namespace to help simplifying the referencing or the syntax. As I explained, in w3school's example proper, it serves zero purpose and can even be taken out.

 
 
subject: Help needed on xmlns & targetNamespace