Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

targetNamespace and xmlns

The page w3schools gives the following as one form of schema declaration.

<?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>

Here,

targetNamespace defines the namespace for the XML document being defined-- which tags (elements) and which attributes can be used in the XML document being defined in "this" XSD.

xmlns=http://www.w3schools.com/schema/schema_schema.asp

, on the other hand, is defining the default namespace for the names in the XML document -- those names that aren't being defined on "this" XSD(?) so, the parser is first looking up the namespace declared in targetNamespace. if it can't find the name in there, going ahead and trying next the one in xmlns (?)

What exactly would i miss if i skip the targetNamespace attribute in the schema declaration above? while I have xmlns, targetNamespace redundant to me since they are referring to the same namespace.

What am i missing?

Note: i've seen What does "xmlns" in XML mean? among some other discussions.

like image 958
Roam Avatar asked Nov 29 '22 15:11

Roam


1 Answers

To understand the difference between targetNamespace and xmlns, just think the following.

XSD is a language to describe XML schemas. Any computer language must be expressed somehow, that is, have some operators, keywords ets. All those things are called grammar.

The authors of XSD (that is W3C) have decided not to invent yet another grammar, but to use XML itself for that. So, XSD is expressed in XML. XML is the carrier for it.

Essentially, it is a sort of coincidence. It was chosen by the XSD authors for convenience (and that convenience does exist indeed!). But, that is not a necessary requirement. For instance, there is another XML schema language called RELAX NG, which is not based on XML.

But once XML is the carrier of all XSD texts, you have to deal with the XML-specific things and xmlns is the one. Basically, it assigns the default namespace for the elements of the given XML file. It has nothing to do with the XML schema that happen to be described in that very file. It is just convention for that XML file (no matter what it contains).

The targetNamespace, on the contrary, is the thing of XSD language itself. It specifies, which namespace the XML elements described by the schema will belong to.

There is some redundancy between targetNamespace and xmlns indeed. But there is no way to use (harness) it, so as to eliminate one of them. Just think this:

XML will be parsed and converted into something else (e.g. XML infoset) by an XML parser. Such a parser is not required to know anything about XSD and its output won't be XML. So, all XML specific things will be lost (that is xmlns, namespace prefixes etc).

Then, that infoset (or something else) is passed to the XSD processor, which starts from anew, and it must have all the necessary information at hand. So, the targetNamespace will be the only thing to tell it about the target namespace of that XML schema!

like image 103
ColdFusion Avatar answered Dec 06 '22 07:12

ColdFusion