Is it valid to have an XML document that uses the same namespace prefix twice?
In the following document the s
prefix is used twice, but in the nested element the URI
is different.
Is it valid?
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Declarations>
<s:MyObject xmlns:s="library://ns.mysite.com">
<s:Paragraph>hello world</s:Paragraph>
</s:MyObject>
</fx:Declarations>
</s:Application>
Yes, prefix has no particular meaning by itself - only as alias to namespace.
Perfectly valid XML with each node name having unique namespace even if all have same prefix:
<a:r xmlns:a="urn:one">
<a:p xmlns:a="urn:two">
<a:c xmlns:a="urn:three">
</a:c>
</a:p>
</a:r>
It also applies to empty (default) prefix which can be changed for each node too. Following is valid XML with each node named with its own namespce even none have prefix:
<r xmlns="urn:one">
<p xmlns="urn:two">
<c xmlns="urn:three">
</c>
</p>
</r>
Note that prefixes in XPath selectors don't relate in to prefixes in any given XML document and must have separate mapping of prefix to namespace. Also in XPath prefixes must have unique mapping (there is no standard for it and mapping is specific for each Xml DOM implementation).
For example for first XML in this post you can't have XPath that look similar to XML like "/a:r/a:p/a:c" as each prefix must map to different namespace. Xpath would look like "/p1:r/p2:p/p3:c" with mapping of prefixes {p1->urn:one, p2->urn:two, p3->urn:three}.