XML
  Home arrow XML arrow Page 3 - Doing More With XML Schemas (part 4)
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 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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

Doing More With XML Schemas (part 4)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2003-02-12

    Table of Contents:
  • Doing More With XML Schemas (part 4)
  • Stocking Up
  • The Name Game
  • Setting Policy
  • Old Friends And New
  • Being Selective
  • Closing Time

  • 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

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Doing More With XML Schemas (part 4) - The Name Game


    (Page 3 of 7 )

    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 namespace 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"?> <ts:inventory> <ts:item ts:name="Mouse C106"> <ts:vendor>Logitech</ts:vendor> <ts:stock>100</ts:stock> </ts:item> <ts:item ts:name="Visor Deluxe"> <ts:vendor>HandSpring</ts:vendor> <ts:stock>23</ts:stock> </ts:item> <ts:item ts:name="Nomad"> <ts:vendor>Creative</ts:vendor> <ts:stock>2</ts:stock> </ts:item> </ts: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.{mospagebreak title=All About Character} Here's an XML document instance which uses namespaces to qualify each element,

    <?xml version="1.0" encoding="UTF-8"?> <sw:gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sw="http://www.somedomain.com/ns/sw/" xsi:schemaLocation="http://www.somedomain.com/ns/sw/starwars.xsd"> <sw:character> <sw:name>Luke Skywalker</sw:name> <sw:species>Human</sw:species> <sw:language>Basic</sw:language> <sw:home>Tatooine</sw:home> </sw:character> <sw:character> <sw:name>Chewbacca</sw:name> <sw:species>Wookie</sw:species> <sw:language>Shyriiwook</sw:language> <sw:home>Kashyyyk</sw:home> </sw:character> <sw:character> <sw:name>Chief Chirpa</sw:name> <sw:species>Ewok</sw:species> <sw:language>Ewok</sw:language> <sw:home>Endor</sw:home> </sw:character> </sw:gallery>
    and here's the corresponding schema.

    <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.somedomain.com/ns/sw/" xmlns:sw="http://www.somedomain.com/ns/sw/" elementFormDefault="qualified"> <!-- define a complex type --> <xsd:complexType name="starWarsEntity"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="species" type="xsd:string"/> <xsd:element name="language" type="xsd:string"/> <xsd:element name="home" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <!-- define the root element and its contents --> <xsd:element name="gallery"> <xsd:complexType> <xsd:sequence> <xsd:element name="character" type="sw:starWarsEntity" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
    The most important line of code in this entire schema is the first line, which defines the namespace used to identify the elements and attributes in the XML document instance.

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.somedomain.com/ns/sw/" xmlns:sw="http://www.somedomain.com/ns/sw/" elementFormDefault="qualified"> <!-- snip --> </xsd:schema> [/code]
    The "targetNamespace" attribute specifies the namespace for this schema, and is what the XML parser/validator uses to differentiate between a <character> in the Star Wars universe, and a <character> from any other source. Note that the namespace URL in the schema matches the namespace URL in the document instance above.

    Within the XML document instance itself, the "sw" namespace is defined at the top of the document as usual, together with a URL that specifies the location of the schema to be used for validation of that namespace (note that this URL is specified via the "schemaLocation" attribute).

    <sw:gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sw="http://www.somedomain.com/ns/sw/" xsi:schemaLocation="http://www.somedomain.com/ns/sw/sw.xsd"> <!-- snip --> </sw:gallery>

    More XML Articles
    More By Harish Kamath, (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 2 hosted by Hostway