TargetNamespace and xmlns without prefix, what is the difference?

In an xml schema document, if I have both targetNamespace and xmlns without a prefix .

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/" xmlns="http://example.com/"> 

What is the difference between the two? My understanding is that if you have xmlns without a prefix, all elements without a prefix will get this namespace and ... confused the same goes for targetNamespace.

+70
xml schema xml-namespaces xsd prefix
Aug 25 '11 at 12:44
source share
7 answers

targetNamespace is an artifact of the XML schema; its purpose: to indicate which specific XML namespace the schema file describes.

xmlns - since the XML schema is an XML document, then you can define a default XML namespace for the XML file itself (this is what the xmlns attribute does); There are several consequences: authorship and composition. For example, you do not need to use a prefix for elements defined in the schema that are later mentioned elsewhere in the same file (for example, a global simple type used as a type for an attribute or element).

In my experience, many XML Schema authors consider this "best practice" ... so you're on the right track.

In terms of XSD, targetNamespace prescribes a portion of the namespace of the qualified name of a schema component, which includes elements, attributes, groups and attribute groups, as well as simple and complex types. Some of the qualified names defined in XSD (elements and attributes) are "directly" used by the XML instance document. Others, for example, for types, can be referenced through the xsi: type attribute in instances of XML documents. The rest (groups, attribute groups) are used to facilitate the compilation of the scheme (through links).

I also believe that (in general) people come up with XSD from two sides:

  • to match existing XML. In this case, if your XML uses namespaces, for each of the namespaces you use, you will get an XSD schema element with the corresponding targetNamespace attribute.

  • pure modeling. Then you think of a targetNamespace that looks like a UML package, or a database schema, or a Java package, or a .NET namespace, and that all means in this case. In fact, this is a mechanism to avoid name collisions; nevertheless, it is also a mechanism for the separation of models in subject areas, etc.

+76
Aug 26 '11 at 4:25
source share

For those who are still confused, consider these three xsds. All of them define one global type and one global definition of an element that refers to it.

First, xsd, like the one that was higher. It uses the prefix 'xsd' for the schema namespace and the default namespace for targetNamespace:

 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/" xmlns="http://example.com/"> <xsd:element name="aGlobalElement" type="aGlobalType"/> <xsd:simpleType name="aGlobalType"> <xsd:restriction base="xsd:string"/> </xsd:simpleType> </xsd:schema> 

Now the same xsd, but defining and using the namespace prefix for the target namespace:

 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/" xmlns:tns="http://example.com/"> <xsd:element name="aGlobalElement" type="tns:aGlobalType"/> <xsd:simpleType name="aGlobalType"> <xsd:restriction base="xsd:string"/> </xsd:simpleType> </xsd:schema> 

... and finally, the version that uses the default namespace instead of "xsd" for the XML schema namespace:

 <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/" xmlns:tns="http://example.com/"> <element name="aGlobalElement" type="tns:aGlobalType"/> <simpleType name="aGlobalType"> <restriction base="string"/> </simpleType> </schema> 

Most schema authors choose the first or the last, because if the default namespace is available, we could use it for something.

+19
Mar 03 '16 at 15:15
source share

Xmlns

The xmlns attribute specifies the default namespace of the described element. Thus, the default namespace applies to all elements within the described element that do not explicitly declare another namespace for themselves.

By default, the namespace is set to the default value for WSDL files: http://www.w3.org/ns/wsdl

Targetnameamespace

This attribute contains the namespace of your web service. You can freely choose this namespace, but there is an agreement that the URI should point to the WSDL of the service.

Xmlns: ths

This namespace must be set to the same URI as the targetNameSpace attribute. This way you can access the target namespace using this namespace prefix (tns).

Source: http://tutorials.jenkov.com/wsdl/description.html

+14
Nov 07 '13 at 10:12
source share

namespace means like scope

targetNamespace is an attribute of a schema element defining a namespace, i.e. package in the XSD file. By convention, we use a URI / URL, but we can use any string.

xmlns is an attribute used to refer to elements and data types that come from the xmlns attribute value for the current element area.

For example:

  • xmlns:xsd="http://www.w3.org/2001/XMLSchema" with a prefix, because xsd means that the namespace must have the xsd: prefix xsd:
  • xmlns="http://www.w3.org/2001/XMLSchema" without default prefix
  • xmlns: p = "http://www.example.com/People" with a prefix, since p means the namespace must have the prefix p:

Where xmlns:xsd and xmlns:p are QNames and xmlns is the local name.

The following image helps to understand XSD using the analogy with Java, as far as I know:

enter image description here

+3
Apr 11 '17 at 2:57 on
source share

None of the above answers made sense to me until I read something on the Microsoft website. Maybe this will help someone understand the difference between "targetNamespace" and "xmlns".

"Xmlns" does one thing: it sets the default namespace for all xml (elements and attributes) INSIDE only your schema file.

"TargetNamespace" performs two functions: it changes the default namespace that you specified using "xmlns" to a new namespace in the schema file AND sets the namespace for all xml documents received from it, and not inside it. The one exception is that its own elements refer to their own types within the schema (see below).

Let's dig into the details to understand what this means:

The "TargetNamespace" in the schema simply tells the parser to move the namespace from the default to the namespace "targetNamespace" , where "xmlns" sets the default namespace before the "targetNamespace" is set. "TargetNamespace" cannot be used until you have set the default namespace using "xmlns".

Example. This works because the default namespace was set first and then changed:

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="abc" targetNamespace="def"> 

This fails because the default namespace is not set at first:

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="def"> 

The above will result in an error because xmlns = 'xxxxx' or the default namespace is not defined. If you add the attribute "targetNamespace" with a value to a schema for which a namespace is not defined - i.e. it has an empty namespace or xmlns = "" - you get an error. What for? You receive an error message because you cannot change the namespace if it has not been installed or is empty. By the way ... xmlns = "" is the same as an empty, empty, or empty namespace in the XML world. But this is the first rule.

It then turns out that each XML document or schema document has an empty default namespace . This is the same as saying xmlns = "". When you add the attribute "xmlns = 'xxxxx'" with a value, you set each tag and attribute in the xml file to a new default namespace. You cannot add "targetNamespace" until it is set, which is a CHANGE in the namespace for each tag in XML.

Let's talk about how the new default namespace is used.

When you set the attribute "xmlns" to a value, you simply say: first set the default namespace using "xmlns". Later, of course, you can change it, even to the same default namespace, using "targetNamespace" as follows:

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="abc" xmlns="abc"> 

The above means that you set the default namespace for the schema and then reset it to the default again. xmlns must be set first, and targetNamespace second. Of course you can make them different.

One note about using a target with an empty namespace. You can add "targetNamespace" as a child link or alias to an empty namespace as follows. This is pretty common:

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="abc" xmlns:tns="abc"> 

But what does that mean? The default namespace remains empty (xmlns = ""), but the alias "tns" is now available as a child of the empty namespace and is used to define specific elements inside your xml using the new targetNamespace. In this scenario, you have an empty namespace, but certain attributes within your schema will have their own namespace using an alias, for example:

 <xs:element name="hello" type='tns:MyType' /> 

This then explains the difference between the two attributes, but does not explain WHY you need both "targetNamespace" and "xmlns".

It turns out that setting your default namespace for your schema, “xmlns = 'xxxxx'”, but NOT setting the target namespace, “targetNamespace = 'xxxxx'”, will install all your xml tags in the new namespace, but will not be allowed outside are xml instances that use a schema or even your schema to access their own types.

Thus, the addition of "targetNamespace" not only changes the default xmlns namespace, but also controls how the scheme accesses its own types. For example, the following code example fails because it lacks targetNamespace. This value is necessary for the schema to assign its simple type to the namespace and gain access to this simple type from its own schema element below. Although a default namespace has been assigned, the custom "simpleType" accessible from the element cannot access it because it was not associated with the target schema namespace. This explains the difference:

 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="abc"> <xsd:element name="myelementname" type="mytype"/> <xsd:simpleType name="mytype"> <xsd:restriction base="xsd:string"/> </xsd:simpleType> </xsd:schema> 

The code below will be fine, since targetSchema has been added and tells the schema that its types are also part of the namespace and are accessible, and the schema can refer to its own types using the new namespace without an alias.

 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="abc" xmlns="abc"> <xsd:element name="myelementname" type="mytype"/> <xsd:simpleType name="mytype"> <xsd:restriction base="xsd:string"/> </xsd:simpleType> </xsd:schema> 

Anyone who has designed this XSD circuit system should return to the drawing board if you ask me. But at least it helps to understand what we're stuck with.

0
Jan 17 '19 at 1:10
source share

The other answers here are good, so I will not repeat their explanations here. However, if someone from the Java background finds it easier, here is an analogy I came up with:

  1. An .xsd document is an artifact / .jar file
  2. xmlns is

     package com.example 

    A statement, you declare at the top of your Java classes.

Consider (for analogy) if you had one package in your Java project and all classes are declared and defined in one external class. For example,

  package com.furniture.models public class FurnitureShop { int noOfTables; int noOfChairs; int noOfBeds; List<Table> tables; List<Chair> chairs; List<Bed> beds; // and now instead of declaring and defining a class for table/chair/bed in a // separate file, you just add it here public static class Table { int height; int width; int length; ... } public static class Chair { String color; ChairType chairType; ... } public static class Sofa { int price; String color; ... } } 

Here's how different elements are grouped into a single .xsd file for a new schema.

  1. targetNamespace is the name of the artifact you are creating. As you can find out for yourself, targetNamespace used when creating the schema in the .xsd file.

After creating an artifact (or .xsd file), it can be used in other projects as follows:

In a Java project, you should import the library using pom.xml (or build.gradle ) as follows:

  <dependency> <groupId>com.furniture</groupId> <artifactId>furniture-apis</artifactId> <version>1.1.1</version> </dependency> 

In XML, you import the schema using

  <furniture xmlns="http://furniture.com"/> 

=== APPENDIX ===

Explanation -

  1. xmlns used as the package statement as well as the import statement in Java. In the .xsd file .xsd xmlns acts like a package statement, while in .xml it acts like an import statement.
0
Jul 05 '19 at 3:23
source share

After rigorous testing using xmllint , I think I found a specific explanation here. Consider the diagram below:

 <?xml version="1.0" encoding="utf-8"?> <xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://yyyzzz.com" xmlns:p="http://abced.com" xmlns:q="http://pqr.com" xmlns="http://yyyzzz.com"> <xsd:element name="recipe" type="recipeType" /> <xsd:complexType name="recipeType"> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="desc" type="xsd:string" /> <xsd:attribute name="archetype" type="xsd:string" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:schema> 

The above diagram validates the following document:

 <?xml version="1.0"?> <recipe xmlns="http://yyyzzz.com"> Deciphering the purpose of targetNamespace </recipe> 

The reason it works is because xmlns = "http://yyyzzz.com" is automatically associated with an element that is also defined by the schema! This means that it also binds to the recipeType element.

Now, with the same xml document, but with a slightly modified layout, as shown below, it also checks and carefully examines the difference:

 <?xml version="1.0" encoding="utf-8"?> <xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://yyyzzz.com" xmlns="http://eigenfield.aparicio.com" xmlns:EGboy="http://yyyzzz.com"> <xsd:element name="recipe" type="EGboy:recipeType" /> <xsd:complexType name="recipeType"> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="desc" type="xsd:string" /> <xsd:attribute name="archetype" type="xsd:string" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:schema> 

Ignore if xmlns is missing, but look carefully at type = "EGboy: recipeType" . We can no longer rely on xmlns because it has different meanings, so we must prefix EGboy with recipeType .

The xml document does not even care about the EGboy prefix, this prefix is ​​intended only to make the schema refer to the corresponding xmlns, if there are many of them.

-one
Mar 15 '17 at 22:28
source share



All Articles