Using Namespaces in XML

Team Fly 

Page 480

Using Namespaces in XML

XML namespaces help prevent ''collisions" that can happen when attribute names or tags are identical if a document contains multiple markup vocabularies (more than one namespace). They are similar to .NET namespaces. This works in XML because each namespace is given a unique number. Commonly, a different URL (Uniform Resource Identifier) is assigned to each namespace. By definition and design URLs are unique—there's only one possible number for each URL anywhere in the world of the Internet. Sometimes a URN (Uniform Resource Number) is used instead. In either case, the number is unique and prevents collisions of the names you use in different vocabularies.

You can either come right out and explicitly name an XML namespace within your XML code, or you can allow the parser to assume the namespace implicitly, by omitting it from your code.

Explicit Declaration

Just as with variables in VB6 and earlier, you can either explicitly declare an XML namespace or let it happen implicitly. Explicit declarations keep things straight if your node contains elements from more than one namespace. You use a shorthand name for the namespace that you use as a prefix (like an alias) to specify which namespace an element belongs to.

 <mo:film xmlns:mo="urn:FilmSociety.com:FilmData"       xmlns:directors="urn:CinemaHistory.com:Directors">   <mo:name>Annie Hall</mo:name>   <directors:director>Woody Allen</directors:director> </mo:film> 

xmlns is the attribute used to declare a namespace and at the same time to specify a prefix that represents the namespace. In the example above, we defined a prefix (mo) that represents the namespace identified by the unique value of "urn:FilmSociety.com:FilmData" and also declared a second namespace ("urn:CinemaHistory.com:Directors") and assigned the word directors as its prefix. Then the mo prefix indicates that the name element belongs to the "urn:FilmSociety.com:FilmData" namespace.

Next the directors prefix specifies that the director element belongs to the "urn:CinemaHistory.com:Directors" namespace. In this way, you can freely employ elements from different namespaces, and not have to worry that you'll run into duplicate (therefore ambiguous) element names. With the prefixes, there will never be confusion if more than one element has the same name.

Implicit Declaration

Implicit declaration means that all the elements inside the element's scope belong to the same namespace (so a prefix is not needed). You accomplish explicit declaration by simply leaving out the prefix when you declare the namespace, like this:

 <film xmlns="urn:FilmSociety.com:FilmData">     <name>Annie Hall</name>     <star>Diane Keaton</star>   </film> 
Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net