Extensible Stylesheet Language (XSL) XML Path Language (XPath) XML example revisited: The XSL Standard. The XML DOM Model

The XSL Standard XSL consists of 3 parts: Extensible Stylesheet Language (XSL) • XPath (navigation in documents) • XSLT (transformation of documents...
Author: Oliver Wilcox
0 downloads 1 Views 272KB Size
The XSL Standard XSL consists of 3 parts:

Extensible Stylesheet Language (XSL)

• XPath (navigation in documents) • XSLT (transformation of documents) • XSLFO (FO for formatting objects) - A rather complex language for typesetting (e.g., for preparing text for printing) - It will not be taught 1

2

XML Path Language (XPath)

XML example revisited:

3

4

The XML DOM Model



document root

The root is implicit

countries

(Does not appear in the text of the XML document)

Israel 6199008

country

Jerusalem Ashdod

continent name

population

city

city

France

Asia

Israel year

name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

60424213 2001



world.xml 5

6

1

The XPath Language

world.dtd

/countries/country[population>10000000] • XPath expressions are used for addressing elements (nodes) of an XML document • Used in XSLT (next subject today) and in XQuery (a query language for XML) • The syntax resembles that of the Unix file system - But the semantics have some substantial differences 7

8

//country[@continent="Asia"]/city world.xml

/countries/country[population>10000000] world.xml















Israel

Israel

6199008

6199008

Jerusalem

Jerusalem

Ashdod

Ashdod









France

France

60424213

60424213







9

XPath Expressions



10

Applying XPath to XML

• An XPath expression (or just XPath for short) matches paths in the XML tree

• Formally, the result of applying an XPath e to an XML document (or a context node in the document) is the list of all nodes n in the

• An absolute path begins with the root of the document

document, such that e matches the path from the root (or the context node) to n

- Starts with "/" (or "//") - For example, /countries/country/city, //city

• The order in the list is defined by the order of the nodes in the document

• A relative path begins with a context node that is defined by the application that uses the XPath - For example, city/name, or ./name

11

12

2

XPath Steps and Axis

Child Axis

• An XPath describes a sequence of steps that together characterize a path

• A child axis has the simple form tagName

• A step is defined by an axis that specifies a tree relationship between nodes

• For example,

- Go to an element child with the tag tagName - /tagName matches the tagName child of root - city/name - /countries/country/city

- More particularly, the axis describes how to get from the current node to the next one - For example, parent-child, child-parent, ancestordescendant, etc.

• The child axis * matches every tag - For example: /*/*/city, */name

• Consecutive steps are separated by / 13

14

ChildAxis Examples Child-/countries

continent name Asia

ChildChild-Axis Examples /countries/country/city

document root

document root

countries

countries

country

country

population

Israel year

city

continent name

name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

2001

city

Asia

population

Israel year

city name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

2001

city

15

16

ChildExamples Child-Axis city/name

Child-Axis Examples Child/*/country/*

document root

document root

countries

countries

An attribute is not country an element child!

Context country

continent name Asia

population

Israel year 2001

city

city

continent name

name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

Asia

population

Israel year 2001

17

city

city

name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

18

3

Self and DescendantDescendant-oror-Self

/countries//name Descendant Examples

• The self axis “.” denotes the identity relationship

document root

- That is, the step “remain in the current node” - /countries/country/. ≡ /countries/country - country/./city ≡ country/city

countries

• The descendant-or-self axis means: either stay in the current node or go to some descendant of the current node - descendant-or-self:node(),

country

continent name

• // is a shotrcut notation for /descendant-orself:node()/

Asia

population

Israel year

- For example, country//name

city name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

2001

city

19

20

.//* Descendant Examples

Other Axis Types

document root

• The parent axis “..” denotes the parent relationship

countries

- That is, the step “go to the parent of the current node” - For example, //name/../population

Context

• XPath has more axis types (denoted by a different syntax from the ones shown earlier):

country

continent name Asia

population

Israel year 2001

city

city

name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

21

-

descendant ancestor ancestor-or-self following-sibling preceding-sibling …

Referring Attributes

22

//country/@continent Attribute Examples

• The attribute axis is denoted @attName

document root

- That is, “go to the attribute attName of the current node” countries

• The operator @* matches every attribute

country

continent name Asia

population

Israel year 2001

23

city

city

name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

24

4

Attribute Examples @continent

Attribute//@* Examples

document root

document root

countries

countries

Context country

continent name Asia

country

population

Israel year

city

continent name

name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

2001

city

Asia

population

Israel year

city name

capital

name

capital

Jerusalem

yes

Ashdod

no

6199008

2001

city

25

XPath Predicates

26

10000000] version="1.0"?> world.xml

• Predicates in XPath are used for filtering out steps

• The XPath

./population is transformed into a number by taking its embedded text

• For example, //city[@captial="yes"] will match only capital cities

Israel

• 6199008 current node (i.e., country) in the path Jerusalem

• Formally, given a predicate [PExpr], the expression PExpr is transformed into a Boolean value and the step is taken only if this value is true - The node reached in the last step is the context node

• Ashdod Equivalent to //country[population>10000000] France

• XPath has a rather rich language for predicate expressions; we only demonstrate common ones

60424213 27

//country[.//city]

world.xml



//country[//city]











28

world.xml

Why?



Israel

Israel

6199008

6199008

Jerusalem

Jerusalem

Ashdod

Ashdod









France

France

An XPath evaluates to true if 60424213 and

60424213

only if its result is not empty

29



30

5

//country[name="Israel" or name="Spain"]/population

//country[population[.>3000000 and @year>2003]]













world.xml



world.xml



Israel

Israel

6199008

6199008

Jerusalem

Jerusalem

Ashdod

Ashdod









France

France

60424213

60424213







//country/city[2]

31

32

Functions

world.xml



• Inside XPath predicates, you can use a set of predefined functions



• Here are some examples:

Israel 6199008

- last() – returns the number of nodes obtained from the last axis step - position() – returns the position of the node in the list

Jerusalem Ashdod

of nodes satisfying the last axis step - name() – returns the name (tag) of the current node - count(XPath) – returns the number of nodes satisfying XPath

acts as an index

• France That is, the number n evaluates to true if n is 60424213 the position of the node among all those reached in the last step (i.e., city)

//country/city[last()]



33

world.xml

34

//city[position()9 and price

Suggest Documents