6

I understand regular XML namespaces such as:

 xmlns:myExample="clr-namespace:WindowsApp.MyNamespace;assembly=MyAssembly"

But I often times see namespaces of the form:

 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

What do these URL namespaces mean? The URLs don't work when I type them into my browser; does anyone know how this works? Thanks in advance.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • See http://stackoverflow.com/tags/xml-namespaces/info – John Saunders May 06 '11 at 16:36
  • @foreyez: read the /info page carefully. Those URLs _are_ XML Namespaces. In fact, the URL kind are the most common kind. – John Saunders May 06 '11 at 16:39
  • I'm not too excited on the explanation on that /info page. Anyone else? – Shai UI May 06 '11 at 16:46
  • @foreyez: it even includes a link to the standard. Did you go look at that? I gather your exposure to XML is through XAML mostly? If you had broader exposure, you'd realize that the way XAML maps XML namespaces to CLR namespaces is unusual. – John Saunders May 06 '11 at 16:48
  • I want a simple answer, not an obscure RTFM answer. And yes, I'm specifically talking about XAML. – Shai UI May 06 '11 at 16:51
  • 2
    @foreyez: that _was_ the simple answer. And did you read the SO question that the info page links to? Sometimes, the only way to avoid RTFM is to duplicate TFM, and why do that? – John Saunders May 06 '11 at 16:55
  • Based on the answer from "[H.B.](http://stackoverflow.com/users/546730/h-b)", I've decided that it's just possible that the OP wasn't just asking about XML namespaces. On that basis, I voted to reopen. – John Saunders May 07 '11 at 23:17
  • @foreyez: were you asking about the URL-flavored namespaces themselves, or were you asking how they are used to map to CLR types? – John Saunders May 07 '11 at 23:18
  • This similar question has a much more constructive answer http://stackoverflow.com/questions/5615168/where-is-mapping-done-for-xmlns-to-the-classes-in-wpf?noredirect=1&lq=1 – Élie Apr 10 '17 at 00:21

2 Answers2

3

See this page on MSDN and the relevant attribute: XmlnsDefinitionAttribute

This attribute is used for clr-mapping by the XAML processor, it allows mapping one or multiple clr-namespaces to a single xmlns and it can be defined in the assembly info.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • MSDN links look exactly like what foreyez is asking about. – DK. May 06 '11 at 22:10
  • @John Saunders: The asker knows how to map to CLR-Namespaces and knows what they are used for, he was wondering how to map to those custom URLs, that's what the `XmlnsDefitionAttribute` is for, and the first link explains a bit more about the XAML mapping. I for one cannot see how this question is supposed to be a duplicate of "[What are XML namespaces for?](http://stackoverflow.com/questions/128389/what-are-xml-namespaces-for)". – H.B. May 07 '11 at 18:09
  • Sorry, but you misunderstood, perhaps because your exposure to XML comes from XAML. It is only in XAML that a namespace "maps" to something. In general, the posted link correctly describes XML namespaces; XAML happens to use certain XML namespaces for a purpose beyond their purpose in the XML standards. – John Saunders May 07 '11 at 18:27
  • @John Saunders: I did not misunderstand and i am aware of how XML namespaces are used otherwise. It's just that all that **does not answer the question at hand** exactly because in XAML there is more behind the namespaces than in regular XML, and since this site is for answering the questions that are being asked my answer is the way it is. – H.B. May 07 '11 at 18:41
  • Here's what you misunderstand. Only _certain_ namespaces in XAML are special, in terms of "mapping" to CLR types. All the rest are just normal XML namespaces and don't map to anything. – John Saunders May 07 '11 at 19:01
  • Remember that XAML _is_ XML. Everything that is true about XML is also true about XAML. Event the "special" namespaces used in XAML are also XML namespaces. They happen to have additional significance when it comes to building CLR types from XAML. These other namespaces have only the significance that any normal XML namespace has. – John Saunders May 07 '11 at 19:03
  • @John Saunders: What are you talking about? If there was *no mapping at all* the compiler would have no way of knowing where to look for the resepctive classes, of course namespaces themselves do not do anything on their own, it's the compiler that does which is common sense. Besides that, this is what MSDN says: *"WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XAML namespace. This attribute, XmlnsDefinitionAttribute"* – H.B. May 07 '11 at 21:13
  • Please go look at the question again. He's not asking about the "clr-*" namespaces. He's asking about namespaces like `xmlns:d="http://schemas.microsoft.com/expression/blend/2008"`. Are you saying that namespace is used to map to a CLR namespace? Which ones? – John Saunders May 07 '11 at 21:15
  • @John Saunders: I know what he is asking and i explained that in an earlier comment. Also as i said in my last comment, namespaces don't do anything on their own, it's just that the compiler maps the ones qualified with `clr-namespace:` directly since all the mapping information is included in the declaration itself while the "normal" ones have to be looked up via the `XmlnsDefitionAttribute` in the respective assemblies. Yes, it is used to map to a namespace, or rather multiple namespaces, [which ones can be found here](http://stackoverflow.com/questions/5615168/). – H.B. May 07 '11 at 21:50
  • Ok, I'll be able to remove the downvote if you'll edit your answer. I recommend that you update it to say what you've just said in the comments - that these namespaces are mapped to CLR namespaces because their assemblies have that attribute on them. – John Saunders May 07 '11 at 21:53
  • @John Saunders: Edited, seriously though, all that which i said can be read in the links i gave, did you even read those? – H.B. May 07 '11 at 21:57
  • No, I didn't. You gave no context at all. – John Saunders May 07 '11 at 21:59
  • @John Saunders: What the hell? Do you think i included the links just for show? – H.B. May 07 '11 at 22:00
  • In my browser, those links don't look like links. They just look like code blocks. – John Saunders May 07 '11 at 22:01
  • @John Saunders: The first one is not even in a code block and the second is not necessary since it just links to the documentation, it's just more convenient if i include it. The question was: *"Url references in namespaces, how do they work?"* and the answer is: *"Via the XmlnsDefinitionAttribute."* That is completely valid on its own, at least in the context of XAML. – H.B. May 07 '11 at 22:04
  • Sorry, that's not actually the answer to the question. He didn't ask "how do URL namespaces map to clr namespaces?" The question was "Url references in namespaces, how do they work?" The answer is that they're just XML namespaces. If he had asked how they mapped, then your answer would be a direct answer. I only removed the downvote because your answer is an _indirect_ answer to the question. – John Saunders May 07 '11 at 22:08
  • It could be read that way, but it's not _necessary_ to read it that way. The OP could clarify it if he wanted. May take was that he wanted to know what those URLs were, considering that they didn't work when he put them into a browser. If he just wanted to know about the mapping, then I'm not sure why he would try to use them as URLs. – John Saunders May 07 '11 at 22:57
  • @John Saunders: Well, it was quite apparent that the asker was clueless about XML namespaces, so pointing out that they in fact are not common URLs might be a good idea as well, but the asker might just as well not care about any of the internals. I noticed your vote already by the way but it probably won't be opened unless one it gets moderator attention... – H.B. May 07 '11 at 23:13
  • Moderators aren't the way to do it. I'm not a moderator, and _I_ voted to reopen. – John Saunders May 07 '11 at 23:16
  • @John Saunders: I disagree with that, i suspect that as soon as you suggested the duplicate-closing all the others jumped on the wagon without considering the depth of the question in the context of WPF, hence i doubt that those votes are very deliberate and hold much merit so bringing this to moderator attention seems like a valid course of action to me. – H.B. May 07 '11 at 23:22
  • Whatever. I'm not that influential. Besides, as I recall, mine was the only "duplicate" vote for some time. Try moderator flags if you want. I just doubt they'll remove close votes. Wouldn't be very democratic. Instead, I added a comment (which brought the question back to the top of the page). – John Saunders May 07 '11 at 23:31
  • @John Sauners: Whatever indeed. – H.B. May 07 '11 at 23:40
1

As John Saunders said, the namespace is the URL. The fact that it's a URL is misleading. The namespace is identified by a URI (of which URL is a subset). The URI is treated as a string. Two namespace identifiers are equal if and only if the strings are equal, so all three of these represent different namespaces:

  • http://www.example.org/~wilbur
  • http://www.example.org/%7ewilbur
  • http://www.example.org/%7Ewilbur

(The example is from the spec: http://www.w3.org/TR/xml-names/)

The namespace serves (as namespaces do) to enable the same name to refer to different things. Thus, you can write XML like this (assuming you have declared namespace prefixes legacy and newSystem):

<newSystem:Type newSystem:TypeName="Customer" newSystem:TableName="Customers" legacy:TableName="cstmr" />

The two TableName elements refer to different things because their namespaces are different.

phoog
  • 42,068
  • 6
  • 79
  • 117