1

I have made an XML generator with a few different options, one is a style option which defines whether to use attributes or elements for primitive data types.

XML schemas aren't supported right now, but I need to allow the setup of an XML Namespace, and I have been doing some research. My understanding is the XML namespace can have a prefix, but it doesn't have to. It also needs a unique string attribute value that is usually a URI, but doesn't have to be.

I am a little bit confused, as I am new to XML namespaces, and I have a few questions about this, if we take a look at an example xml document

<?xml version="1.0"?>
<root xmlns="some_identifier">
<oneKey>value</oneKey>
</root>

This is using an element style of the key value example, and I'm going to allow the configuration of "some_identifier". In this example is the "oneKey" element under the XML namespace? Or do I have to specify a prefix like xmlns:ns and then prefix "oneKey" with "ns"?

Also if we take a look at attribute style:

<?xml version="1.0"?>
<root xmlns="some_identifier" oneKey="value" />

Do we need to define a similar prefix in oneKey in this example?

I apologize if I'm way off the mark in these questions, please let me know if I'm just not making sense,

UPDATE:

I have found this site: http://www.rpbourret.com/xml/NamespacesFAQ.htm#exec_1

That says these two are identical:

<foo:A xmlns:foo="http://www.foo.org/">
     <foo:B>abcd</foo:B>
</foo:A>

and

<A xmlns="http://www.foo.org/">
     <B>abcd</B>
</A>

which is very useful, but regarding the attribute style. Do I need to prefix attributes? Or will the xmlns default work for these as well?

Doug Molineux
  • 12,283
  • 25
  • 92
  • 144

1 Answers1

2
<root xmlns="some_identifier">

this declares default namespace, root and all its children will belong to it, you don't need prefixes.

<root xmlns="some_identifier" oneKey="value" />

Default namespaces do not apply to attributes. If you want oneKey to belong to a namespace, you have to create a prefix and use it on the attribute

<root xmlns="some_identifier" xmlns:myns="some_identifier" myns:oneKey="value"/>
panda-34
  • 4,089
  • 20
  • 25
  • Ty for your reply. When you say `Default namespaces do not apply to attributes.` Does this only apply to attributes in the root element? Or does it apply to all attributes in all subelements as well? – Doug Molineux Jun 06 '12 at 15:48
  • Sorry, but I have one more question: the official doc says `Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.` So if by default elements under the default namespace are considered to be in the scope of the namespace, then so would the attributes of the same element? So you wouldn't have to prefix each attribute name? – Doug Molineux Jun 06 '12 at 16:01
  • Just read a couple more sentences beyond that. You'll find out that `The namespace name for an unprefixed attribute name always has no value`. Your quote says about interpretation, meaning that element's namespace is enough to determine what to do with its attributes even if they don't have their own namespace. – panda-34 Jun 06 '12 at 16:20
  • Anyway, you can safely write `` whereas this would be an error: `` – panda-34 Jun 06 '12 at 16:21
  • Ah! `always has no value` brilliant, sorry I had to uncheck the answer for a bit, I wanted you to come back ;) – Doug Molineux Jun 06 '12 at 16:28