XML
  Home arrow XML arrow Page 5 - XML Basics (part 2)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XML Basics (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2001-07-28

    Table of Contents:
  • XML Basics (part 2)
  • Splitting Up
  • Eating Humble PI
  • XML And Alcohol
  • The Name Game
  • An Entity In The Attic
  • Digging For Treasure
  • The Man From IDIOT
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    XML Basics (part 2) - The Name Game


    (Page 5 of 9 )

    Setting up a namespace is simple - here's the syntax:

    <elementName xmlns: prefix="namespaceURL">
    In this case, the prefix is the unique string used to identify the namespace; this is linked to a specific namespace URL.

    A namsepace is usually declared at the root element level, although authors are free to declare it at a lower level of the tree structure too.

    Once the namespace has been declared within the document, it can be used by prefixing each element within that namespace with the unique namespace identifier. Take a look at my revised stock portfolio, which now uses a "mytrades" namespace to avoid name clashes.

    <mytrades:portfolio xmlns:mytrades="http://www.somedomain.com/namespaces/mytrades/"> <mytrades:stock>Cisco Systems</mytrades:stock> <mytrades:stock>Nortel Networks</mytrades:stock> <mytrades:stock>eToys</mytrades:stock> <mytrades:stock>IBM</mytrades:stock> </mytrades:portfolio>
    And once Tom gets over his hangover, I guess he'd find it pretty easy to fix his document too.

    <?xml version="1.0"?> <toms_store:inventory xmlns:toms_store="http://www.toms_store.com/"> <toms_store:category>Mice</toms_store:category> <toms_store:item>Mouse C106</toms_store:item> <toms_store:vendor>Logitech</toms_store:vendor> <toms_store:stock>100</toms_store:stock> <toms_store:category>Handhelds</toms_store:category> <toms_store:item>Visor Deluxe</toms_store:item> <toms_store:vendor>HandSpring</toms_store:vendor> <toms_store:stock>23</toms_store:stock> <toms_store:category>MP3 players</toms_store:category> <toms_store:item>Nomad</toms_store:item> <toms_store:vendor>Creative</toms_store:vendor> <toms_store:stock>2</toms_store:stock> </toms_store:inventory>
    In case you're wondering, the namespace URL is simply a pointer to a Web address, and is meaningless in practical terms; the XML specification doesn't really care where the URL points, or even if it's a valid link.

    In case a single document contains two or more namespaces, adding namespace declarations and prefixes to every element can get kind of messy - as the following example demonstrates.

    <?xml version="1.0"?> <me:person xmlns:me="http://www.mywebsite.com/"> My name is <me:name>Huey</me:name>. I'm <me:age>seven</me:age> years old, and I live in <me:address>Ducktown</me:address> with <rel:relationships xmlns:rel="http://www.mywebsite.com/relationships/">my brothers <rel:name>Dewey</rel:name> and <rel:name>Louie</rel:name></rel:relationships>. </me:person>
    In such a situation, XML allows you to specify any one namespace as the default namespace, by omitting the prefix from the namespace declaration. Modifying the document above to make "me" the default namespace, we have

    <?xml version="1.0"?> <person xmlns="http://www.mywebsite.com/"> My name is <name>Huey</name>. I'm <age>seven</age> years old, and I live in <address>Ducktown</address> with <rel:relationships xmlns:rel="http://www.mywebsite.com/relationships/">my brothers <rel:name>Dewey</rel:name> and <rel:name>Louie</rel:name></rel:relationships>. </person>
    Namespaces need not be restricted to elements alone - attributes can use namespaces too, as the following example demonstrates.

    <?xml version="1.0"?> <mystuff:collection xmlns:mystuff="http://www.somedomain.com/mystuff" xmlns:music="http://www.somedomain.com/music_genres"> <mystuff:artist>Spears, Britney</mystuff:artist> <mystuff:title music:genre="Pop">Oops, I Did It Again!</mystuff:title> </mystuff:collection>


    This article copyright Melonfire 2001. All rights reserved.

    More XML Articles
    More By icarus, (c) Melonfire


     

       

    XML ARTICLES

    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway