CMP 051 XML Introduction Session IV Chapter 12 – XML Namespaces Chapter 13 – Using Namespaces http://www.profburnett.com
Outline Name Conflicts Uniform Resource Identifier (URI) Designing a Namespace Declaring a Default Namespace - The xmlns Attribute Solving the Name Conflict Using a Prefix Populating a XML Namespace Referencing XML Schema Components Locally Defined Elements Multiple Files Multiple Namespaces Schema of Schemas XSLT and Namespaces 8/1/2014 Copyright © Carl M. Burnett
Name Conflicts HTML Table XML <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> <table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> 8/1/2014 Copyright © Carl M. Burnett
Uniform Resource Identifier (URI) URI - Uniform Resource Identifier URL - Uniform Resource Locator URN - Uniform Resource Name URL URN http://www.profburnett.com/2014/spring/2014_Spring_Session_I.html Directories File Name Domain Name Protocol 8/1/2014 Copyright © Carl M. Burnett
Designing a Namespace Use your own domain name Must be Unique Must be Persistent Add Versioning Use a URL 8/1/2014 Copyright © Carl M. Burnett
Declaring a Default Namespace The xmlns Attribute syntax. xmlns:prefix="URI". <course xmlns:”http://profburnett.com/ns/xml/1.0”> <course> <title>XML Intro </title> <CRN>35108</CRN> <dept> WDCE</dept> </course> 8/1/2014 Copyright © Carl M. Burnett
Solving the Name Conflict Using a Prefix <root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3schools.com/furniture"> <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root> <root> <h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table xmlns:f="http://www.w3schools.com/furniture"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root> 8/1/2014 Copyright © Carl M. Burnett
Populating a XML Namespace XSD <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.kehogo.com/ns/wow/1.0"> <xs:element name="ancient_wonders"> <xs:complexType> <xs:sequence> <xs:element name="wonder" type="wonderType" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="wonderType"> <xs:element name="name" type="nameType" <xs:element name="location" type="xs:string"/> <xs:element name="height" type="heightType"/> 8/1/2014 Copyright © Carl M. Burnett
XML Schema and Documents <?xml version="1.0"?> <ancient_wonders xmlns="http://www.kehogo.com/ns/wow/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.kehogo.com/ns/wow/1.0 13-01.xsd"> <wonder> <name language="English">Colossus of Rhodes</name> <name language="Greek">Κολοσσός της Ρόδου</name> <location>Rhodes, Greece</location> <height units="feet">107</height> <history> <year_built era="BC">282</year_built> <year_destroyed era="BC">226</year_destroyed> <how_destroyed>earthquake</how_destroyed> 8/1/2014 Copyright © Carl M. Burnett
Referencing XML Schema Components To declare a default Namespace for XML Schema Components xmlns=“URI” To declare a Namespace with a prefix for XML Schema Components xmlns:prefix=“URI” To then reference those component in the XML Schema Prefix:component_name 8/1/2014 Copyright © Carl M. Burnett
Locally Defined Elements To add all locally defined elements to the targeted Namespace In the xs:schema element elementFormDefault=”qualified” To add all locally defined attributes to the targeted Namespace In the xs:schema element attributeFormDefault=”qualified” 8/1/2014 Copyright © Carl M. Burnett
Locally Defined Elements To add all particular locally defined element to the targeted Namespace In the element definition form=”qualified” To keep a particular locally all locally defined element from being associated with the targeted Namespace In the element definition form=”unqualified” 8/1/2014 Copyright © Carl M. Burnett
Multiple Files To include XML Schema components in multiple files Directly after the xs:schema element add: <xs:includeschemaLocation=”includefile.uri”/> 8/1/2014 Copyright © Carl M. Burnett
Multiple Namespaces To import components from XML Schemas with different target namespaces Directly after the xs:schema element in the imported document add: <xs:inport namespace=“URI” schemaLocation=”schema.uri”/> 8/1/2014 Copyright © Carl M. Burnett
Schema of Schemas To declare the Schema of Schemas as the default namespace: After the XML Declaration: <schema xmlns=“http://www.w3.org/2001/XMLSchema” Continue to identify target namespaces, then end the shema declaration tag with a > Copyright © Carl M. Burnett 8/1/2014
XSLT and Namespaces To use a XML namespace in an XSLT style sheet: Within the opening tag of the root element xsl:stylesheet type: xmlns:prefix=“URI” Label individual elements by typing: <prefix:element> 8/1/2014 Copyright © Carl M. Burnett
Review Name Conflicts Uniform Resource Identifier (URI) Designing a Namespace Declaring a Default Namespace - The xmlns Attribute Solving the Name Conflict Using a Prefix Populating a XML Namespace Referencing XML Schema Components Locally Defined Elements Multiple Files Multiple Namespaces Schema of Schemas XSLT and Namespaces Next – Chapter 16 – XQuery 1.0 8/1/2014 Copyright © Carl M. Burnett