質問

The attribute expression xmlns="" declares the "no namespace"-namespace.

For example:

...
<html xmlns="http://www.w3c.org/xhtml">
   <table><!--table of hotels-->
      ...
      <td>
         <description xmlns="">
            <name>Las Vegas</name>
            <city>Phoenix, Arizona</city> 
         </description>
      </td>
      ...
   </table>
</html>

In this example I just use it to annotate some data with elements that do not belong to any namespace.

Which makes sense to me as I don't have to find or create an appropriate namespace. Maybe sometimes you don't want to work out a schema if you won't reuse it and there's only few data to annotate with it anyway.

It would also make sense if the document above was just the first sketch before the schema design and xmlns="" elements are just used to comment on the data for future changes.

What's the common practice? Isn't it almost ever used?

役に立ちましたか?

解決

Clarifying: Your document is xhtml document, and has set that as its default namespace (xmlns="http://www.w3c.org/xhtml) What the xmlns="" attribute does is to say that this default no longer applies, and that the element it appears on and its children are not to be understood as xhtml but as raw xml with no default namespace.

Yes, this syntax is widely used wherever it is necessary to cancel a default namespace (though arguably it'd be slightly better form to not set the default namespace in the first place and use prefixes on your names to bind them to namespaces).

As to whether you should be using "no namespace" in the first place: That's up to you. Namespaces are recommended for any nontrivial document type because they make it possible at some point in the future to intermix elements from that document type with elements from another document type and make sense out of the combination. Basically, if you don't use namespaces then there's a risk that your use of <name> for a hotel will get entangled with someone else's use of <name> for a human or a variable or gods-know-what.

Without namespaces, you'd have to seriously consider always writing something like <TRAVEL_AGENT_DATA_HOTEL_name> to avoid that hazard. Being able to just write <hotel:name>, and bind the hotel: prefix to the namespace for travel-agent documents really is a huge improvement.

XML Namespaces were created because they were needed. You will use them, because you will need to use them.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top