43

What is targetNamespace's function?

<schema xmlns="http://www.w3.org/2001/SchemaXML"
        targetNamespace="http://www.example.com/name"
        xmlns:target="http://www.example.com/name">

I understand that xmlns="http://www.w3.org/2001/SchemaXML defines the Schema XML namespace.

I also understand that xmlns:target="http://www.example.com/name" defines the namespace for my own vocabulary if I'm creating my own schema using the prefix "target"; this acts as a proxy or placeholder for the URI http://www.example.com/name.

That seems like enough to define the needed boundries and vocabularies of namespace participants. So why do I need a targetNamespace attribute which duplicates the http://www.example.com/name namespace?

Anthony Faull
  • 17,549
  • 5
  • 55
  • 73
jojo10
  • 567
  • 2
  • 5
  • 7

3 Answers3

49

Answered quite well over here: targetNamespace and xmlns without prefix, what is the difference?

To restate:

  • targetNamespace="" - As the current XML document is a schema this attribute defines the namespace that this schema is intended to target, or validate.

  • xmlns="" - Defines the default namespace within the current document for all non-prefixed elements (i.e no yada: in <yada:elementName>)

  • xmlns:target="" - here you are just defining your own namespace with the prefix target:, this is unrelated to the previous two special cases.

Community
  • 1
  • 1
Shaun
  • 1,293
  • 16
  • 19
  • 6
    `this attribute defines the namespace that this schema is intended to target` I find this so confusing. The schema is "targeting" a namespace? So if I make a schema with `targetNamespace="google.com"` I've just created a namespace on Google? Then what? Will it mess up other people's schemas who've done the same? Also, what prefix is used? – CodyBugstein Jan 07 '16 at 00:53
  • 1
    @Imray it is confusing, I think of it as XSL document is defining an XML structure, the namespace is simply a way of uniquely identifying this structure so that later on when an XML document references it (using xmlns= or xmlns:somelabel= as shown above) it has a name that doesn't conflict with other XSL documents you might be referencing, does that make sense? – Shaun Jan 22 '16 at 11:48
  • Is `targetNamespace` of a `.xsd` usually be the same as `xsi` of the `.xml`s it validates? I know that `xsi` is conventional. – Minh Nghĩa May 23 '20 at 21:46
  • 1
    @MinhNghĩa xsi and xsd are namespace prefixes, but you are right they are conventionally used to refer to two specific things. Please check this answer for a discussion of the differences between them: https://stackoverflow.com/a/41041892/276874 – Shaun Mar 02 '22 at 13:09
8

The targetNamespace declares a namespace for other xml and xsd documents to refer to this schema. The target prefix in this case refers to the same namespace and you would use it within this schema definition to reference other elements, attributes, types, etc. also defined in this same schema definition.

s_t_e_v_e
  • 2,496
  • 3
  • 31
  • 35
2

The prefix "target" in xmlns:target="http://www.example.com/name" is nothing special. How would a schema processor know that you wanted that to be the target namespace for your schema? targetNamespace does just that - it declares the namespace that components of your schema belong to.

N.B. Not everything in the schema document goes into the targetNamespace. Note attributes "elementFormDefault" and "attributeFormDefault" on the "schema" element and also attribute "form" on the "element" and "attribute" elements.

Kevin
  • 1,876
  • 12
  • 17