xml & related languagesxml essentials (part 2) © 2003 john e. arnold all rights reserved. 1 xml...

Post on 22-Dec-2015

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

1

XML & Related Languages

Unit 2

XML Essentials (part 2)

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

2

In Unit 1, we looked at…

• XML document Structured data in text format Elements Attributes Must be well-formed

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

3

In Unit 2…

• DTDs Document Type Definitions Letting us specify a document structure

for a particular use part of the eXtensibility of XML

• Validation Does a document conform to our structure? Compare to well-formed

• Namespaces Preventing naming conflicts and confusion

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

4

DTDs

• Document Type Definitions

Define the legal structure of an XML Document…… for a particular application

Goes above and beyond being well-formed

Describes structure, content, and quantities of Elements Attributes Entities

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

5

DTDs (part 2)

• Allow verification that a particular XML document is consistent with a known specification

• Independent developers sharing a DTD will know that their XML documents are consistent and compatible (i.e., share the same data structure)

• Enable validation

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

6

DTDs (part 3)

• DTDs are different from XML Schema Older More widely used today

• XML Schema are the W3C recommended replacement for DTDs New development should probably use XML

Schema Understanding DTDs will make understanding

XML Schema much easier

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

7

DTDs & Validation

• XML parser Reads content of XML document Reports error if not well-formed

• Validating parser XML parser and Validates an XML document against its

related DTD (or XML schema) Reports an error if an inconsistency occurs

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

8

Declaring a DTD

• For an XML document to be validated with respect to a DTD, the document must declare its DTD Part of the prolog to the XML document Inline

DTD is fully defined in the XML document itself External reference

External reference to the DTD itself (URL) More useful: they can be shared across apps

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

9

Inline DTD

<?xml version="1.0"?><!DOCTYPE rootElement [

<!-- All DTD content here -->]>

<rootElement></rootElement>

• Note that the type being declared (rootElement) must match the root element of the document must be declared as an element in the DTD

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

10

External DTD Reference

• Public reference Provide information about the owner of the

DTD spec (e.g., W3C) Provide location of DTD

• Non-public reference Provide location of DTD only

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

11

External DTD Reference – Public

<?xml version="1.0" standalone="no"?><!DOCTYPE rootElement PUBLIC

"ownerString" "locationString">

<rootElement></rootElement>

• Note that the type being declared (rootElement) must match the root element of the document must be declared as an element in the DTD

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

12

External DTD Reference – Public

• Example: SVG file<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"><svg>

<!-- content here --></svg>

• Owner String structure separates categories with // + or – tells whether it's a recognized standard (e.g., ISO) Organization Descriptive label Language code (ISO 639-1)

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

13

External DTD Reference – Non-Public

<?xml version="1.0" standalone="no"?><!DOCTYPE rootElement SYSTEM

"locationString">

<rootElement></rootElement>

• Note that the type being declared (rootElement) must match the root element of the document must be declared as an element in the DTD

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

14

External DTD Reference – Non-Public

• Example: our own Document Type<?xml version="1.0" standalone="no"?><!DOCTYPE sample SYSTEM

"http://www.brain-salad.com/DTDs/sample.dtd"><sample>

<!-- content here --></sample>

• Location String Note: Validation requires access to the file in this string

Not just a unique string like a namespace (that we'll see later)

URL: fully qualified or relative File: relative to current directory

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

15

DTDs: On with the Show

• You'll notice… DTDs are not defined in XML syntax

XML Schemas are

DTDs are not that easy to read But you can get the hang of it

Most browsers don't use their parsers as validating parsers To check our results, we really should use a program

that calls a validating parse» Ex: a tool like XmlSpy

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

16

DTD: Element Declarations

• Establishes a valid element name• Describes what the element can contain

<!ELEMENT elementName (elementContent)>

elementName: tag that will be used elementContent: name(s) of allowed child

element(s) in parentheses

(more in a bit)

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

17

DTD Element Declaration Example

<!ELEMENT baseballTeam (name, city, player*)><!ELEMENT name (#PCDATA)><!ELEMENT city (#PCDATA)><!ELEMENT player (#PCDATA)>

• <baseballTeam> contains exactly 1 name element followed by exactly 1 city element followed any number of player elements (note the * meaning 0 or more) Note that this enforces ordering and quantity of the elements that

are contained

• <name>, <city>, and <player> contain only text (#PCDATA). They cannot contain other elements.

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

18

DTD: Elements #PCDATA

• PCDATA parsed character data #PCDATA is a reserved token in DTDs to

mean that an element contains character text 0 or more characters <city>Boston</city>

» Boston is the character text <!ELEMENT city (#PCDATA)>

Since there's no element name in the parentheses, <city> can only contain the parsed character data.

Other elements would not be allowed

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

19

DTD: Elements element content

• Specified in parentheses What elements (if any) are allowed Order of the elements Quantity of each element

• Which elements are allowed? Only those element names listed

and all of those listed must be defined as elements in the DTD

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

20

DTD: Elements element content (part 2)

• Order of contained elements If you want to specify the exact order in which

they appear… Separate by commas

» Ex: (name, city)

• Quantity of each element element exactly once element+ 1 or more times element? 0 or 1 time element* 0 or more times

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

21

DTD: Elements choices & parentheses

• Sometimes, you want to provide a choice between alternatives that can be contained by an element… Vertical bar (|) – like a logical ‘exclusive or’

<!ELEMENT gender (male | female) >or

<!ELEMENT coin (penny | nickel | dime| quarter) >

Can't repeat an element in the choice

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

22

DTD: Elements choices & parentheses

• Can use additional parentheses to show additional groupings and/or choices

<!ELEMENT FruitBasket (Orange, Grapefruit?,(Apple | Orange)+) >

Exactly 1 Orange element, followed by 0 or 1 Grapefruit elements, followed by 1 or more additional elements (where each

additional element is either an Apple element or an Orange element)

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

23

DTD: Elements mixed-content elements

• Mixed-content elements can contain character data and other elements

• DTD doesn't allow complete freedom in design of mixed content: Use #PCDATA at the beginning of a group Indicating a choice between #PCDATA and 1 or

more elements that can be contained in the element

Make sure the group is declared as having 0 or more cardinality (*)

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

24

DTD: Elements mixed-content elements

<!ELEMENT paragraph(#PCDATA | highlight | term)*>

• <paragraph> can contain parsed character data, <highlight> elements, and <term> elements in any order can't specify how many of each can't specify order in which they appear

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

25

DTD: Elements empty & any

• Empty element element can appear but must be empty

Contains neither elements nor character data<!ELEMENT myEmptyElement EMPTY>

• Element that can contain anything Still needs to be well-formed Can be useful for holding XML for which you don't

have the DTD or for which you haven't finished the DTD

<!ELEMENT myAnythingElement ANY>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

26

DTD: Attribute List Declarations

• Define: Which attributes can appear in an element's start-

tag Type of the attribute (in a limited sense) Whether the attribute is required and/or default

values

<!ATTLIST elementattr_name attr_type attr_defaultattr_name2 attr_type2 attr_default2...>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

27

DTD:Attributes names

• Must be legal attributes names

• Any number of attributes… But each name must be unique for the

element

• Order of declaration doesn't matter Since order they appear in XML doesn't

matter

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

28

DTD:Attributes types

• Used to restrict values of attributes 10 types 4 most commonly used

CDATA Any legal XML character data NMTOKEN Contains only characters in a legal

XML name. Used to preventwhitespace.

ID Legal XML name and unique

within the document for this

element/attribute in thisdocument. Like a key.

enumerated Specific list of values in parentheses separated by |

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

29

DTD:Attributes required/default

• 4 choices to indicate whether attribute is required and/or default value #REQUIRED Attribute must be included in the

element's start tag. Use its value. #IMPLIED Attribute is optional. No default

value. #FIXED literal Attribute is optional. If in element

start-tag, value must match literal.

If not in start-tag, value is assumed

to be literal. literal Attribute is optional. If not in start-

tag, value defaults to literal

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

30

DTD:Attributes – An Example

<!ATTLIST Employee

Badge ID #REQUIRED

WorkLocation CDATA "HOP1"

PayType ( Salary | Hourly | Contract ) #REQUIRED>

• These are the attributes allowed for Employee elements The Badge attribute has type ID and is required The WorkLocation attribute allows any string data, is optional,

and has a default value of “HOP1” The PayType attribute value must be either “Salary”, “Hourly”, or

“Contract” and the attribute is required.

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

31

DTD: Entities

• Remember the 5 built-in entity references? (ex: &gt; and &amp;)

• DTDs let you define your own Then, you can use them in your

documents Like macros that expand to a particular

value<!ENTITY entName stringValue>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

32

DTD: Entities (part 2)

• Example definition (in.dtd)<!ENTITY copyright "<copyright><year>2002</year><who>John Arnold</who></copyright>">

• Example use (in .xml)<footer>&copyright;</footer>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

33

DTD: Entities External Entities

• Let you define an entity in another file Like an include (text replacement)

If it contains markup, it must be well-formed

Need to be able to access it!

<!ENTITY extCopyright SYSTEM"http://www.brain-salad.com/XML/copyright.xml">

Usage is the same: &extCopyright;

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

34

DTD: Entities Parameter Entities

• Internal and External forms• Are used only within the DTD itself

Text that's repeated throughout the DTD that you don't want to have to retype in multiple places

Additional syntax: %Internal: <!ENTITY % entName entValue>

External: <!ENTITY % entName SYSTEM entLocation>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

35

DTD: Entities Parameter Entities

• Example definition (in .dtd)<!ENTITY % attrWorkLocations"WorkLocs (HOP1|HOP2|MIL1) #REQUIRED">

• Example use (in .dtd)<!ATTLIST customer %attrWorkLocations;

anotherAttr CDATA #IMPLIED>

• Note: Can't use in the .xml file that refers to .dtd!

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

36

DTD Example

• Goal: XML representation for employees There are 0 or more employees Each employee has the following:

First Name Last Name Badge Number

» must be unique Job Code and description:

» Code might be all numeric Pay type

» Salary, Hourly, or Contract Work location:

» optional. If not given, assume "HOP1"

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

37

DTD Example (part 2)

• Questions to ask ourselves given the requirements Elements vs attributes What can we model with DTDs and what can't we model?

• Start with root element and elements it might contain

<!ELEMENT Employees (Employee*)>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

38

DTD Example (part 3)

• Employee has first and last name• Badge needs to be unique

Hmmm… sounds like a job for an ID attribute

• Job Code and Description Sounds like an element with an attribute might work here

• Pay type 3 "legal" values. Sounds like we might need an enumerated

attribute

• Work location has an optional value Another place where an attribute might work best

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

39

DTD Example (part 4)

• Add the other elements we need Remember every element you refer to

must be declared

<!ELEMENT Employee (FirstName, LastName, JobCode)>

<!ELEMENT FirstName (#PCDATA)><!ELEMENT LastName (#PCDATA)><!ELEMENT JobCode (#PCDATA)>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

40

DTD Example (part 5)

• Add the attributes we need

<!ATTLIST EmployeeBadge ID #REQUIREDWorkLocation CDATA "HOP1"

PayType ( Salary | Hourly | Contract ) #REQUIRED>

<!ATTLIST JobCode Code NMTOKEN #REQUIRED>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

41

DTD Example (part 6)

• Connecting the XML Document to the DTD … with a DOCTYPE in the XML Prolog

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Employees SYSTEM "DTDs\35employees.dtd"><Employees>

<Employee Badge="_123" PayType="Hourly"><FirstName>John</FirstName><LastName>Doe</LastName><JobCode Code="404">Software

Engineer</JobCode></Employee><Employee PayType="Salary" Badge="_456" WorkLocation="HOP2">

<FirstName>Jane</FirstName><LastName>Smith</LastName><JobCode Code="909">Dir. of

Engineering</JobCode></Employee>

</Employees>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

42

DTD Example (part 7)

• Validating the XML Document Just because we added the DOCTYPE

doesn’t mean it’s valid! DOCTYPE provides the association and

location of the DTD A Validating parser (or equivalent tool) is

needed to actually determine if the XML document is valid

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

43

Namespaces in XML

• A W3C Recommendation after XML 1.0 Namespaces in XML, Jan. 1999 http://www.w3.org/TR/REC-xml-names/

Therefore, the oldest parts of XML were designed before the work on namespaces was finished DTDs, etc.

Newer parts of XML (e.g., XSLT, XPath, XML Schema) fully integrate with namespaces

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

44

Why Namespaces?

• What if… (#1)

I define the name element like this:<name><firstName>John</firstName><middleInitial>E</middleInitial><lastName>Arnold</lastName>

</name>

… but you define it like this:<name>John E Arnold</name>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

45

Why Namespaces? (part 2)

• If I am going to define a structure that uses a name element, which would I expect to encounter? Do I need to be prepared for both? Can I specify which one I want to allow?

Use namespaces to differentiate!

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

46

Why Namespaces? (part 3)

• What if… (#2)

We need to integrate our application (employee info) with a system management application that defines its data with this:<server mfg="Compaq" model="DL380"><domain>qa.MyCompany.com</domain><name>test01</name><managedBy>

<name>John E. Arnold</name></managedBy>

</server>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

47

Why Namespaces? (part 4)

• What if… (#2 continued)

Can we define our document to allow only the 'hardware' form of name in one place and only the 'person' form of name in the other?

Use namespaces to differentiate!

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

48

Namespaces

• Allow you to divide your structure into application-specific groups Ex: This data came from (or is destined for) the

employee application

• Allow you to resolve collisions that arise from 2 designers using the same name for different data Ex: This data is structured using the definition

from the system management application

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

49

Namespaces (p. 2)

• Without namespaces Elements defined (and compared and validated)

in terms of their name<ElementA> = <ElementA>

The same as having an empty string as its namespace (the ‘no namespace’ namespace)

• With namespaces Elements defined (and compared and validated)

in terms of namespace and name<NS1:ElementA> = <NS2:ElementA> ???

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

50

Namespaces (p. 3)

• Q: Are these two elements ‘equal’ ???<NS1:ElementA> = <NS2:ElementA>

• A: It depends… The element names (to the right of the colon)

must be identical The namespaces must be identical, too.

But… the string to the left of the colon is an alias for a namespace and not the namespace itself!

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

51

Namespaces: Example<?xml version="1.0"?><qaLabResponsibilities

xmlns:hw="http://www.brandeis.edu/XmlCourse/hardware"xmlns:people="http://www.brandeis.edu/XmlCourse/roster"><hw:servers>

<hw:server hw:mfg="Compaq" hw:model="DL380"><hw:domain>qa.MyCompany.com</hw:domain><hw:name>test01</hw:name><hw:managedBy>

<people:name>John E. Arnold</people:name></hw:managedBy>

</hw:server></hw:servers><hw:routers></hw:routers>

</qaLabResponsibilities>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

52

Namespace: Uniqueness

• Namespaces are strings.• Any element or attribute with the same name in the

same namespace should be referring to an element or attribute of the same ‘type’ Same structure, same semantics, etc.

• Namespace names refer to data that is structured for one purpose or application Collisions can occur unless you are absolutely certain that

the name of your namespace will not used to model any other reason

The same namespace can be (and is) used in multiple documents… But the name we choose should always refer to the same data

structuring

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

53

Namespace: Uniqueness (part 2)

• Use unique URI (uniform resource indicator) for namespace names

Ex: http://www.brandeis.edu/XmlCourse/hw

mailto:jarnold@brandeis.edu

• Best practice Use a domain you own Add on unique identifier for each use

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

54

Namespace: Uniqueness (part 3)

• Use of URIs does not mean that you need to be connected to the network! They're really just unique strings Don't assume that there really is a web

page that is associated with the URI It's just a convenient string that minimizes

chance of reuse Not like external DTD declarations that

must be accessible in order to work

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

55

Declaring Namespaces

• To use a namespace, you need to declare it Declare it like an attribute (in the start tag) using

one of 2 forms xmlns declares a default namespace

xmlns:alias declares a namespace alias

Can declare a namespace in any element in an XML document

Scoping rule: Namespace is declared only in the element in which it is

declared and its descendants

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

56

Declaring Namespaces (p. 2)

• Declaring without an alias:<BookProjects

xmlns="http://www.codenotes.com/NS/BPs"><Editor>Gregory Brill</Editor><Titles>

<Title>CodeNotes for XML</Title><Title>CodeNotes for .NET</Title><Title>CodeNotes for Java</Title>

</Titles></BookProjects>

Declares a default namespace

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

57

Declaring Namespaces (p. 3)

• Default namespace There is no alias with which you can refer

to the namespace The element in which the default

namespace is declared is put into the default namespace Unless the element is explicitly defined as

being in another namespace (using an alias)

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

58

Declaring Namespaces (p. 4)

• Declaring with an alias:<lists:classList

xmlns:lists="http://www.brandeis.edu/Lists"><Instructor>John Arnold</Instructor><Students>

<Student>Jane Doe</Student><Student>John Smith</Student>

</Students></lists:classList>

Defines a namespace aliasCan declare more than one alias

in an element’s start tag

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

59

Declaring Namespaces (p. 5)

• Namespace alias Declares a short name that can be used to refer to the

namespace The alias is used to the left of the colon to put an element

(or attribute) into the associated namespace<NS1:ElementA>contents of the element</NS1:ElementA>

Refers to an element named ElementA in the namespace associated with the NS1 alias

The namespace is the full string, not the alias string! Two different alias strings can resolve to the same

namespace string Sometimes <NS1:ElementA> and <NS2:ElementA> can refer

to the same element in the same namespace!

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

60

Declaring Namespaces (p. 6)

• Namespace alias (continued)

Unlike a default namespace, the declaration of an alias does not assign that namespace to the element in which the alias is declared The declaration makes it eligible for use… … it does not automatically put it into use

<Name xmlns="urn:myName"/>is not the same as

<Name xmlns:myNS="urn:myName"/>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

61

Declaring Namespaces (p. 7)

• Why allow use of alias as a prefix? Less typing (since the actual namespace string can be quite

long) Some characters in the namespace aren’t allowed in XML

tag names …But remember the key difference … a namespace with an alias doesn't get used until you

prefix an element (or attribute) with it Thus, declaring a namespace prefix in an Element definition

does not mean that Element is automatically in the namespace (see use of myNS in previous example)

• Once an element is in a namespace, what about its child elements?

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

62

Namespace Declaration ‘Inheritance’

• Namespace declarations are ‘inherited’ Apply to the element in which they are defined and to all

elements within the content of that element Exception 1: an alias can be redefined to a different namespace in a

child element by re-using the alias in a namespace declaration Exception 2: a default namespace can be changed in a descendant

element by redefining the default namespace

• Ramifications Once declared, a namespace alias can be used to be put any child

elements or their attributes into the namespace Once a default namespace is declared, all descendant elements

will be put into that default namespace unless (a) the element is prefixed with a namespace alias or (b) the default namespace is overridden with a new default namespace

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

63

Namespace Declaration ‘Inheritance’ (p. 2)

<employee:person xmlns="urn:myDefaultNS" xmlns:employee="urn:EmployeeNS">

<name><firstName>John</firstName><middleInit>E</middleInit><lastName>Arnold</lastName>

</name><employee:info>

<badge>2003</badge><department>Continuing Studies</department><employee:title>Instructor</employee:title>

</employee:info></employee:person>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

64

Namespace Declaration ‘Inheritance’ (p. 3)

• Namespace declaration ‘inheritance’ is a relationship between elements, not attributes!

• Attributes are not in a namespace (that is, their namespace string is empty) unless the attribute name is prefixed with a namespace alias and a colon

<Name xmlns:myNS="urn:myName"><First myNS:nickname="Tom" nickname="Tommy">Thomas</First>

</Name>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

65

A Namespace Example

<outside xmlns="http://www.ourdomain.com/NS/outer"><middle myAttr="myValue">some character data <inside

xmlns:inner="http://www.ourdomain.com/NS/inner">

<inner:core>More character data </inner:core>

</inside></middle>

</outside>

• outside, middle, and inside are in the default namespace• core is in the inner namespace• myAttr is not in either namespace – it's an attribute without

an explicitly specified namespace

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

66

Using A Namespace Alias

• Once you've declared a namespace alias, you associate an element (or attribute) with that namespace by using prefix: Result is a qualified name

• Use of prefix in start-tag requires use of prefix in end-tag. 2 identical element names in different

namespaces are considered different elements

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

67

Another Namespace Example

<Instrumentation xmlns="urn:myInstruments"xmlns:prop="urn:myProperties"xmlns:uom="urn:myUnitsOfMeasure"><Pitch>

<prop:Speed uom:units="mph">46</prop:Speed></Pitch><Bat>

<prop:Speed uom:units="fps">47</prop:Speed></Bat><Hit>

<prop:Distance uom:units="ft">95</prop:Distance></Hit>

</Instrumentation>

• <Instrumentation>, <Pitch>, <Bat>, and <Hit> are in the default namespace ( "urn:myInstruments" )

• <Speed> and <Distance> are in the urn:myProperties namespace• The units attributes are in the urn:myUnitsOfMeasure namespace

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

68

Namespaces with Attributes

• As previously discussed, attributes can be assigned a namespace, too.

But namespaces for attributes need to be explicitly specified. The 'Namespaces in XML' W3C Recommendation says:

"The namespace declaration is considered to apply to the element where it is specified and to all elements within the context of that element, unless it is overridden by another namespace declaration…"

"A default namespace is considered to apply to the element where it is declared (if that element has no namespace prefix), and to all elements with no prefix within the content of that element."

"Note that default namespaces do not apply directly to attributes."

The DOM Level 2 spec says: Note: Per the Namespaces in XML Specification [Namespaces] an attribute does

not inherit its namespace from the element it is attached to. If an attribute is not explicitly given a namespace, it simply has no namespace.

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

69

Namespaces with Attributes (part 2)

The examples in the recommendation also indicate that attributes are not automatically put in the same namespace as the element for which the attribute is specified "[…] each of the following is legal because the default

namespace does not apply to attribute names."

<!– http://www.w3.org is bound to n1 and is the default -->

<x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org"> <good a="1" b="2" /> <good a="1" n1:a="2" /></x>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

70

Namespaces with Attributes (part 3)

"Because attributes must be unique on a per element basis, you rarely need to assign namespaces to attributes"(XML Pocket Consultant, p. 97)

But: some XML applications define attributes that are expected to be in a particular namespace Ex: for validation against a range of values If you need to do this, you use the same syntax as

assigning a namespace to an element: prefix:attributeName

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

71

When will namespaces matter?

• When we move to validation of XML

• When we write code that is looking for particular elements and/or attributes that are in a given namespace

(i.e, soon)

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

72

Namespaces: Reminder

• Declaring a namespace prefix (xmlns:???) does not assign that namespace to the element in which the namespace is declared

<?xml version="1.0"?><catalog xmlns:cat="http://www.books.com/ns/cat">This is my catalog.</catalog>

is not the same thing as

<?xml version="1.0"?><cat:catalog

xmlns:cat="http://www.books.com/ns/cat">This is my catalog.</cat:catalog>

XML & Related Languages XML Essentials (part 2)© 2003 John E. Arnold All Rights Reserved.

73

Unit 3…

• Begin looking at ways XML can be transformed To convert from one representation to

another Data transformation Output/Presentation transformation

First stop: XSLT and XPath These will use namespaces!

top related