XML Namespaces - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

XML Namespaces

Description:

XML Namespaces Andy Clark The Problem Documents use different vocabularies Example 1: CD music collection Example 2: online order transaction Merging multiple ... – PowerPoint PPT presentation

Number of Views:106
Avg rating:3.0/5.0
Slides: 18
Provided by: AndyC69
Learn more at: http://people.apache.org
Category:
Tags: xml | namespaces | suffix

less

Transcript and Presenter's Notes

Title: XML Namespaces


1
XML Namespaces
  • Andy Clark

2
The 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?

3
The 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

4
Namespace 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

5
Example Document (1 of 3)
  • Namespace binding

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
6
Example Document (2 of 3)
  • Namespace scope

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
7
Example Document (3 of 3)
  • Bound elements

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
8
Important 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

9
Using 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

10
Parameter 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
11
Parameter 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
12
Parameter 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
13
Parameter Entity Trick Step 4
  • Declare namespace binding attribute for all
    possible root elements

12 lt!ATTLIST order xmlns CDATA
http//example.com/NSgt
14
Add 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
15
Use 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
16
Useful 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

17
XML Namespaces
  • Andy Clark
Write a Comment
User Comments (0)
About PowerShow.com