views:

809

answers:

3

Hello everyone,

Suppose I have the following XML schema file and the following XML document file. I have two questions,

  1. Since there is no target name space specified in XML Schema file, what namespace will Information element in?

  2. In the XML document file, when using Information, which namespace does it belong to? Please notice in this case, I do not refer to XML Schema file from the XML document file.

XML Schema file:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Information" type="xs:string"/>
</xs:schema>

XML document file:

<?xml version="1.0" encoding="utf-8"?>
<Information>Hello XML</Information>

thanks in advance, George

A: 

Since both files are unrelated (you do not reference the schema file from your xml file), the Information element will be in the default (empty) namespace.

Ronald Wildenberg
@rwwilden, confirm with you that when we refer an element, if we do not refer by any namespace prefix ns:element, the element will always be in default(empty) namespace?
George2
That's correct. Without a prefix the element is always in the default namespace.
Ronald Wildenberg
I think you have a bit of confusion here - as dommer notes, the default namespace and no (described as empty above) are different things. Also, the default namespace in a schema and the default namespace in an xml instance are not the same thing (confusingly)
Nic Gibson
@rwwilden, have you referred to what dommer mentioned and quoted above? Seems default namespace and empty namespace are two different things. And in my case Information element should be defined in empty namespace?
George2
@newt, 1. in my sample, Information element should be defined in no namespace? 2. If the answer to 1 is yes, how could we refer to Information element from another XML document file if it is defined in no namespace?
George2
(continued) I am very interested in how to define default namespace in XML schema and default namespace in XML document file. Could you show us a simple samples please?
George2
In that case, I may be incorrect. I didn't know there was a difference between the empty and the default namespace...
Ronald Wildenberg
@rwwilden, no problem, appreciate if you could share some thoughts here when you have good ideas. Let us just wait on others' comments.
George2
+2  A: 

The Information element will be in no namespace. To put it in a default namespace you would have to specify that namespace in the tag.

<Information xmlns="http://www.mydefaultnamespace.com"&gt;

From an Oracle article:

No Namespace

No namespace exists when there is no default namespace in scope. A {default namespace} is one that is declared explicitly using xmlns. When a {default namespace} has not been >declared at all using xmlns, it is incorrect to say that the elements are in {default >namespace}. In such cases, we say that the elements are in {no namespace}. {no namespace} >also applies when an already declared {default namespace} is undeclared.

Here's a pretty comprehensive namespace resource:

XML Namespaces FAQ

dommer
@dommer, I think in my case, Information element in XML schema file is defined in no namespace. Another question, how will we refer such element (in no namespace) in XML document file?
George2
I'm not sure you can refer to it explicitly. What would you refer to?There's no namespace. I think you'd have to assign a default namespace to be able to refer to anything explicitly - but I'm prepared to be proven wrong.
dommer
@null, I am very interested in how to define default namespace in XML schema and default namespace in XML document file. Could you show us a simple samples please?
George2
+1  A: 

Why are you defining a Schema for no target? It does not make sense.

leppie
@leppie, 1. do you mean if there is no target namespace defined in schema file, there is no way to refer such defined scehma elements in XML document? 2. if we do not specify target namespace in XML schema file, what namespace will defined element in -- in default namespace or in no namespace?
George2
Hi George :) 1. Yes. 2. Nothing, it's just meaningless text in verbose brackets. It depends what you are using to read the XML. XML is useless with schema. You could however predefine the default schema in your given XML validating reader, and that would avoid you having to define the default namespace. Not giving a namespace is almost like anonymous classes in C#, in a lexical sense.
leppie
Cool answer, leppie! I am interested in -- "You could however predefine the default schema in your given XML validating reader, and that would avoid you having to define the default namespace.", you mean there is a way to define default schema in validating reader other than in XML document itself by using xmlns? If yes, could you show me a sample please? 2. "Not giving a namespace is almost like anonymous classes in C#" -- you mean not specifying target namespace (continue)
George2
of XML schema file, or you mean not defining namespace in XML document?
George2