Title: XML Namespaces
1XML Namespaces
2The Problem
- Documents use different vocabularies
- Example 1 CD music collection
- Example 2 online order transaction
- Merging multiple documents together
- Name collisions can occur
- Example 1 albums have a ltnamegt
- Example 2 customers have a ltnamegt
- How do you differentiate between the two?
3The Solution Namespaces!
- What is a namespace?
- A syntactic way to differentiate similar names in
an XML document - Binding namespaces
- Uses Uniform Resource Identifier (URI)
- e.g. http//example.com/NS
- Can bind to a named or default prefix
4Namespace Binding Syntax
- Use xmlns attribute
- Named prefix
- e.g. ltafoo xmlnsahttp//example.com/NS/gt
- Default prefix
- e.g. ltfoo xmlnshttp//example.com/NS/gt
- Element and attribute names are qualified
- URI, local part (or local name) pair
- e.g. http//example.com/NS , foo
5Example Document (1 of 3)
01 lt?xml version1.0 encodingUTF-8?gt 02
ltordergt 03 ltitem codeBK123gt 04
ltnamegtCare and Feeding of Wombatslt/namegt 05
ltdesc xmlnshtmlhttp//www.w3.org/1999/xhtmlgt 0
6 The lthtmlbgtbestlt/htmlbgt book ever
written! 07 lt/descgt 08
lt/itemgt 09 lt/ordergt
6Example Document (2 of 3)
01 lt?xml version1.0 encodingUTF-8?gt 02
ltordergt 03 ltitem codeBK123gt 04
ltnamegtCare and Feeding of Wombatslt/namegt 05
ltdesc xmlnshtmlhttp//www.w3.org/1999/xhtmlgt 0
6 The lthtmlbgtbestlt/htmlbgt book ever
written! 07 lt/descgt 08
lt/itemgt 09 lt/ordergt
7Example Document (3 of 3)
01 lt?xml version1.0 encodingUTF-8?gt 02
ltordergt 03 ltitem codeBK123gt 04
ltnamegtCare and Feeding of Wombatslt/namegt 05
ltdesc xmlnshtmlhttp//www.w3.org/1999/xhtmlgt 0
6 The lthtmlbgtbestlt/htmlbgt book ever
written! 07 lt/descgt 08
lt/itemgt 09 lt/ordergt
8Important Points
- Namespace scope is the element and descendents
from point of binding - Attributes are not in elements namespace
- Unless implicitly prefixed
- Can not unbind named prefixes
- However, you can unbind default prefix
9Using Namespaces with DTDs
- The problem
- DTD syntax does not support namespaces
- The solution
- Use a namespace-aware schema language
- Use parameter entity trick to add simple
namespace support to existing DTDs
10Parameter Entity Trick Step 1
- Define parameter entities
- Prefix, suffix, and xmlns parameter entity
- Xmlns parameter entity
01 lt!ENTITY prefix ''gt 02 lt!ENTITY suffix
''gt
03 lt!ENTITY xmlns xmlnssuffixgt
11Parameter Entity Trick Step 2
- Define element name parameter entities
- One for every element name in grammar
04 lt!ENTITY order prefixorder
gt 05 lt!ENTITY item prefixitemgt 06 lt!ENTITY
name prefixnamegt 07 lt!ENTITY price
prefixpricegt
12Parameter Entity Trick Step 3
- Modify all element declarations to reference
element names by parameter entity
08 lt!ELEMENT order (item)gt 09 lt!ELEMENT
item (name,price)gt 10 lt!ELEMENT name
(PCDATA)gt 11 lt!ELEMENT price (PCDATA)gt
13Parameter Entity Trick Step 4
- Declare namespace binding attribute for all
possible root elements
12 lt!ATTLIST order xmlns CDATA
http//example.com/NSgt
14Add Namespace Information to Existing,
Un-prefixed Documents
- Existing documents gain namespace info
01 lt?xml version1.0 encodingEBCDIC?gt 02
lt!DOCTYPE order SYSTEM grammar.dtdgt 03
ltordergt 04 ltitem codeBK123gt 05
ltnamegtCare and Feeding of Wombatslt/namegt 06
ltprice currencyUSDgt42.00lt/pricegt 07
lt/itemgt 08 lt/ordergt
15Use New Prefix with Same DTD
- Redefine prefix, suffix in DTD internal subset
01 lt?xml version1.0 encodingEBCDIC?gt 02
lt!DOCTYPE aorder SYSTEM grammar.dtd 03
lt!ENTITY prefix agt 04 lt!ENTITY suffix
agt 05 gt 06 ltaorder xmlnsahttp//example.
com/NSgt 07 ltaitem codeBK123gt 08 lt!--
... --gt
16Useful Links
- XML 1.0 Specification
- http//www.w3.org/TR/REC-xml
- Namespaces in XML Specification
- http//www.w3.org/TR/REC-xml-names
- XHTML 1.0 Specification
- http//www.w3.org/TR/xhtml1
17XML Namespaces