Namespaces and Introduction to XPath

R. Alexander Milowski

milowski@sims.berkeley.edu

School of Information Management and Systems

#1

Motivation for Namespaces

#2

Problems - Example 1

#3

Problems - Example 2

#4

Solutions?

#5

Names and Namespaces

#6

Prefixes and Namespaces

#7

Defaulting Namespaces

#8

Namespace Names and URI Values

#9

Declarations have Scope

#10

What's a Name?

#11

The Infoset "Changes"

#12

The Infoset "Changes" - Element

#13

The Infoset "Changes" - Attribute

#14

How it Works - An Example

The simplest thing is to default the namespace:

<order xmlns="urn:publicid:IDN+cde.berkeley.edu:example:order:en">
  <from>me</from>
  <to>you</to>
  <items>
    <item>A clue</item>
    <item>A better clue!</item>
  </items>
  <memo>
    <p>I need this order now!</p>
  </memo>
</order>

#15

How it Works - An Example - Part 2

But we can use a prefix and get the exact same names:

<o:order xmlns:o="urn:publicid:IDN+cde.berkeley.edu:example:order:en">
  <o:from>me</o:from>
  <o:to>you</o:to>
  <o:items>
    <o:item>A clue</o:item>
    <o:item>A better clue!</o:item>
  </o:items>
  <o:memo>
    <p>I need this order now!</p>
  </o:memo>
</o:order>

#16

How it Works - An Example - Part 3

If we change that prefix binding later on, we'll get different names:

<o:order xmlns:o="urn:publicid:IDN+cde.berkeley.edu:example:order:en">
  <o:from>me</o:from>
  <o:to>you</o:to>
  <o:items xmlns:o="urn:publicid:IDN+cde.berkeley.edu:example:order:items:en">
    <o:item>A clue</o:item>
    <o:item>A better clue!</o:item>
  </o:items>
  <o:memo>
    <p>I need this order now!</p>
  </o:memo>
</o:order>

#17

How it Works - An Example - Part 4

We can also mix in a default if we wish:

<o:order xmlns:o="urn:publicid:IDN+cde.berkeley.edu:example:order:en">
  <o:from>me</o:from>
  <o:to>you</o:to>
  <o:items xmlns:o="urn:publicid:IDN+cde.berkeley.edu:example:order:items:en">
    <o:item>A clue</o:item>
    <o:item>A better clue!</o:item>
  </o:items>
  <o:memo xmlns="http://www.w3.org/1999/xhtml">
    <p>I need this order now!</p>
  </o:memo>
</o:order>

#18

Namespace Info Item

#19

In-Scope Namespaces

#20

And now for something completely different...

We're now actually going to do something with XML...

...but we need some tools.

#21

XPath

#22

Like Directory Paths

#23

Node Set Results

#24

Selecting Attributes

#25

Names and Namespaces

#26

No Prefix = No Namespace

#27

Wildcards

#28

Context Node

#29

Parent and Ancestors

#30

Conditional Matching

#31

Skipping Levels

#32

Special Functions

#33

The Real Story

#34

Trees and XML

For the computer scientists:

#35

Relationships in a Tree

Relationships from the red node.

#36

Additional XML Relationships

#37

Axes are Directions on Relationships

  • Axes are just a traversal of a relationship.

  • Some are tree relationships:

    • ancestor, ancestor-or-self

    • parent, child, self

    • descendant, descendant-or-self

    • following, following-sibling

    • preceding, preceding-sibling

  • And some extras:

    • attribute

    • namespace

#38

Axis Syntax

#39

Axis Specifics

#40

Principal Node Type

#41

Axis Direction

#42

Axis Direction - Example

For example, give the following

<doc>
<a/><b/><c/>
<target/>
<d/><e/><f/>
</doc>

These expressions evaluate:

target/preceding-sibling::* → elements 'c' b' 'a'.

target/following-sibling::* → elements 'd' 'e' 'f'.

#43

Abbreviated Syntax Equivalences

Abbreviation Equivalence
../name
parent::name
name
child::name
//name
descendant::name
.
self::node()
*
child::*
@*
attribute::*
@name
attribute::name

#44

What use is this?

#45

Next Time