Home arrow XML arrow Page 5 - Doing More With XML Schemas (part 4)

Old Friends And New - XML

In this concluding article, find out all about namespaces - what they are, how they work, and how you can use schemas to make sure that they're enforced in a consistent manner.

  1. Doing More With XML Schemas (part 4)
  2. Stocking Up
  3. The Name Game
  4. Setting Policy
  5. Old Friends And New
  6. Being Selective
  7. Closing Time
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 7
February 12, 2003

print this article


The XML Schema specification also notes the existence of an "attributeFormDefault" attribute of the <xsd:schema> element, which lets schema authors do to attributes what the "elementFormDefault" attribute does for elements - force qualification of each attribute with a namespace prefix. Here's an example that demonstrates how this works:

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema targetNamespace="http://www.somedomain.com/ns/sw/" xmlns:sw="http://www.somedomain.com/ns/sw/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="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:attribute name="episode" type="xsd:string" /> </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>
I've introduced a new attribute here for the <character> element - "episode" - which lists the Star Wars episode in which the character first appeared. My use of the "attributeFormDefault" attribute ensures that every occurrence of this attribute in an XML document instance will be prefixed with a namespace - as you can see in the following XML document:

<?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:episode="IV"> <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:episode="IV"> <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:episode="VI"> <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>

>>> More XML Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: