2

I'm wondering how does XML namespaces work. I read they should be unique etc. But none of resources stated how they exactly work.

What I mean i have XML node like

<name xml:lang="en" type="abc">A name</name>

And to extract xml:lang with php (SimpleXML) i need to use following code:

$obj->attributes('http://www.w3.org/XML/1998/namespace');

How does this link define/tell SimpleXML to extract xml:* attributes? I Only see web page there, not single definition that would define this namespace.

As How do XML namespaces work states URLs are never resolved. But document i parse does not contain this link (namespace definition).

Also, in question above, there are defined namespaces. Where they define that this namespace is exactly human:* ?

Community
  • 1
  • 1
Grzegorz
  • 3,538
  • 4
  • 29
  • 47

1 Answers1

2

"How does this link define/tell SimpleXML to extract xml:* attributes? I Only see web page there, not single definition that would define this namespace."

xml: is a special prefix that predefined in the XML specification. It is the prefix to which namespace name http://www.w3.org/XML/1998/namespace is bound, and this is how that 'link' connected to the prefix xml. See : W3C : The "xml:" Namespace.

"Also, in question above, there are defined namespaces. Where they define that this namespace is exactly human:* ?"

The prefix human is not declared anywhere in that XML. I believe that was by mistake though, as the XML is not well-formed in its current form. The prefix declaration should've been xmlns:human instead of xmlns:html at the human:body element :

<human:body xmlns:human="http://www.example.com/human/">
    <human:height>182 cm</human:height>
    <human:weight>83 kg</human:weight>
</human:body>
har07
  • 88,338
  • 12
  • 84
  • 137
  • So basically `xmlns:human="http://www.example.com/human/"` defines XML Namespace `human` identified (identifier used for extraction/accss) by `http://www.example.com/human/` identifier? – Grzegorz Dec 08 '15 at 13:13
  • 1
    @Gacek yup, that part defines the mapping of prefix `human` to the namespace name/uri `http://www.example.com/human/` – har07 Dec 08 '15 at 13:15