<p>I'm trying to understand how namespaces work in XML. When I have an element like foo:bar, the attributes will often not have namespaces on them. But sometimes they will. Are the attribute in the namespace of the element, even when the default namespace has been declared? Looking over the xsd for xhtml it seems the attributes are part of the schema and should be in the namespace for xhtml, but they are never presented that way...</p>

[{'answer_id': 41577, 'author': 'Staale', 'author_id': 3355, 'author_profile': 'https://Stackoverflow.com/users/3355', 'pm_score': 2, 'selected': False, 'text': '<p>Read up at <a href="http://www.w3.org/TR/REC-xml-names/#scoping" rel="nofollow noreferrer">6.1 Namespace Scoping</a> and <a href="http://www.w3.org/TR/REC-xml-names/#defaulting" rel="nofollow noreferrer">6.2 Namespace Defaulting</a> on w3c.</p>\n\n<p>Basically:</p>\n\n<blockquote>\n  <p>The scope of a namespace declaration declaring a prefix extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag</p>\n</blockquote>\n\n<p>However, the text here doesn\'t seem to explain if  means a is foo:a or the default namespace in the context. I would assume that it does not refer to foo:a, but rather the documents default namespace a. Considering this quote at least:</p>\n\n<blockquote>\n  <p>Such a namespace declaration applies to all element and attribute names within its scope whose prefix matches that specified in the declaration. </p>\n</blockquote>\n\n<p>Ie. the namespace "foo:" only applies to elements prefixed with foo:</p>\n'}, {'answer_id': 43226, 'author': 'jelovirt', 'author_id': 2679, 'author_profile': 'https://Stackoverflow.com/users/2679', 'pm_score': 4, 'selected': False, 'text': '<p>Examples to illustrate using the <a href="http://www.jclark.com/xml/xmlns.htm" rel="noreferrer">Clark notation</a>, where the namespace prefix is replaced with the namespace URL in curly brackets:</p>\n\n<pre><code>&lt;bar xmlns:foo="http://www.foo.com/"\n    foo:baz="baz"\n    qux="qux"/&gt;\n&lt;bar xmlns="http://www.foo.com/" xmlns:foo="http://www.foo.com/"\n    foo:baz="baz"\n    qux="qux"/&gt;\n&lt;foo:bar xmlns="http://www.foo.com/" xmlns:foo="http://www.foo.com/"\n    foo:baz="baz"\n    qux="qux"/&gt;\n</code></pre>\n\n<p>is</p>\n\n<pre><code>&lt;{}bar\n    {http://www.foo.com/}baz="baz"\n    {}qux="qux"/&gt;\n&lt;{http://www.foo.com/}bar\n    {http://www.foo.com/}baz="baz"\n    {}qux="qux"/&gt;\n&lt;{http://www.foo.com/}bar\n    {http://www.foo.com/}baz="baz"\n    {}qux="qux"/&gt;\n</code></pre>\n'}, {'answer_id': 46865, 'author': 'Bart Schuller', 'author_id': 4711, 'author_profile': 'https://Stackoverflow.com/users/4711', 'pm_score': 6, 'selected': False, 'text': '<p>Most of the time, attributes will not be in any namespace. The <a href="http://www.w3.org/TR/REC-xml-names/#defaulting" rel="noreferrer">namespace spec</a> says (<em>emphasis</em> mine):</p>\n\n<blockquote>\n  <p>A default namespace declaration applies to all unprefixed <em>element</em> names within its scope. Default namespace declarations do <em>not</em> apply directly to <em>attribute</em> names; the interpretation of unprefixed attributes is determined by the element on which they appear.</p>\n</blockquote>\n\n<p>There\'s a reason that most XML vocabularies use non-namespaced attributes:<br>\nWhen your elements have a namespace and those elements have attributes, then there can be no confusion: the attributes belong to your element, which belongs to your namespace. Adding a namespace prefix to the attributes would just make everything more verbose.</p>\n\n<p>So why do namespaced attributes exist?<br>\nBecause some vocabularies do useful work with mostly attributes, and can do this when mixed in with other vocabularies. The best known example is <a href="http://en.wikipedia.org/wiki/XLink" rel="noreferrer">XLink</a>.</p>\n\n<p>Lastly, W3C XML Schema has an all too easy way (<code>&lt;schema attributeFormDefault="qualified"&gt;</code>) of declaring your attributes as being in a namespace, forcing you to prefix them in your documents, <em>even</em> when you use a default namespace.</p>\n'}, {'answer_id': 235551, 'author': 'Diego Tercero', 'author_id': 2046272, 'author_profile': 'https://Stackoverflow.com/users/2046272', 'pm_score': 4, 'selected': False, 'text': '<p>There\'s something related to this attributes/namespaces subject that took me some time to understand today when I was working on a XSD. \nI\'m going to share this experience with you in case anyone ever happens to have the same issues.</p>\n\n<p>In the Schema Document I was working on there were a couple of global attributes referenced by some elements. To simplify things here let\'s assume this XSD I\'m talking about was about a <strong>Customer</strong>.</p>\n\n<p>Let\'s call one of these global attributes <strong>Id</strong>. And the root element using it <strong>Customer</strong></p>\n\n<p>My XSD declaration looked like this :</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;\n&lt;xs:schema  xmlns="http://schemas.mycompany.com/Customer/V1" \ntargetNamespace="http://schemas.mycompany.com/Customer/V1" \nxmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;\n</code></pre>\n\n<p>My <em>Id</em> attribute declaration looked like this :</p>\n\n<pre><code>&lt;xs:attribute name="Id" type="xs:positiveInteger"/&gt;\n</code></pre>\n\n<p>And my <strong>Customer</strong> element used the attribute like this :</p>\n\n<pre><code>&lt;xs:element name="Customer"&gt;\n   &lt;xs:complexType&gt;\n      &lt;xs:attribute ref="Id" use="required"/&gt;\n      &lt;!-- some elements here --&gt;\n    &lt;/xs:complexType&gt;\n&lt;/xs:element&gt;\n</code></pre>\n\n<p>Now, let\'s say I wanted to declare a <strong>Customer</strong> XML document like this :</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;\n&lt;Customer Id="1" xmlns="http://schemas.mycompany.com/Customer/V1"&gt;\n  &lt;!-- ... other elements here --&gt;\n&lt;/Customer&gt;\n</code></pre>\n\n<p>I found out that I can\'t : when the attribute is globally declared, it\'s not in the same namespace than the element who references it.</p>\n\n<p>I figured out that the only solution with the XSD defined like that was to declare the namespace twice: once without a prefix in order to make it the default namespace for elements, and once with a prefix in order to use it with the attributes. So this is how it would have looked like :</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;\n&lt;Customer cus:Id="1" xmlns="http://schemas.mycompany.com/Customer/V1"\n xmlns:cus="http://schemas.mycompany.com/Customer/V1"&gt;\n  &lt;!-- ... other elements here --&gt;\n&lt;/Customer&gt;\n</code></pre>\n\n<p>This is so unpractical that I just decided to get rid of all global attributes and declare them them locally. Wich in the case of the example I gave here would have looked like this:</p>\n\n<pre><code>&lt;xs:element name="Customer"&gt;\n   &lt;xs:complexType&gt;\n       &lt;xs:attribute name="Id" type="xs:positiveInteger" use="required"/&gt;\n       &lt;!-- some elements here --&gt;\n   &lt;/xs:complexType&gt;\n&lt;/xs:element&gt;\n</code></pre>\n\n<p>I found it hard to find some references about what I\'m talking about here in the net. I eventually found <a href="http://xsd.stylusstudio.com/2002Feb/post08016.htm" rel="noreferrer">this post</a> in the Stylus XSD Forum where a guy named Steen Lehmann suggested either to declare the attribute locally or to declare it within an attribute group </p>\n\n<blockquote>\n  <p><em>"so that the attribute declaration\n  itself is no longer global"</em></p>\n</blockquote>\n\n<p>This last solution has a "hacky" taste, so I just decided to stick with the first solution and declare all my attributes locally.</p>\n'}]
['https://Stackoverflow.com/questions/41561', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4315/']