问题
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?
回答1:
<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"/>
来源:https://stackoverflow.com/questions/10917416/configuring-an-xml-namespace