Doing More With XML Schemas (part 4) - Setting Policy
(Page 4 of 7 )
You can tell the validator to ensure that every element in the XML document instance is qualified with a namespace via the "elementFormDefault" attribute of the <xsd:schema> element.
<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>
This "elementFormDefault" attribute tells the validator that every element in
the document instance must be qualified with a namespace identifier in order for the document to be valid. In order to verify this, you can try removing the namespace prefix from one of the <character> elements in the document instance above and validating the XML data - your validator should throw up a bunch of error messages about improperly-qualified elements.
Document authors can also satisfy this qualification requirement by specifying a default namespace for all the elements in the document instance - as in the following code segment.
<?xml version="1.0" encoding="UTF-8"?>
<gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.somedomain.com/ns/sw/"
xsi:schemaLocation="http://www.somedomain.com/ns/sw/starwars.xsd">
<character>
<name>Luke
Skywalker</name>
<species>Human</species>
<language>Basic</language>
<home>Tatooine</home>
</character>
<character>
<name>Chewbacca</name>
<species>Wookie</species>
<language>Shyriiwook</language>
<home>Kashyyyk</home>
</character>
<character>
<name>Chief
Chirpa</name>
<species>Ewok</species>
<language>Ewok</language>
<home>Endor</home>
</character>
</gallery>
If you don't necessarily want to force the document author to prefix the namespace
identifier to every element in each document instance, you can turn off this requirement by specifying a value of "unqualified" for the "elementFormDefault" attribute.
Next: Old Friends And New >>
More XML Articles
More By Harish Kamath, (c) Melonfire