75
1 Dr Alexiei Dingli XML Technologies X-Languages

1 Dr Alexiei Dingli XML Technologies X-Languages

Embed Size (px)

Citation preview

Page 1: 1 Dr Alexiei Dingli XML Technologies X-Languages

1

Dr Alexiei Dingli

XML Technologies

X-Languages

Page 2: 1 Dr Alexiei Dingli XML Technologies X-Languages

2

• X Link

• X Pointer

• X Path

• X Query (Brief)

• X Form (Brief)

• XSLT

• FO

Contents

Page 3: 1 Dr Alexiei Dingli XML Technologies X-Languages

3

X Languages

X Query XPointer XLink

X Path

XSLT

Page 4: 1 Dr Alexiei Dingli XML Technologies X-Languages

4

• Standard way of defining hyperlinks in XML

• Short for XML Linking

• Similar to HTML Links but more powerful

• Any element can have an XLink

X Link

Page 5: 1 Dr Alexiei Dingli XML Technologies X-Languages

5

• In HTML we use the <A> tag

• In XML all tags can have a link

<homepages xmlns:xlink="http://www.w3.org/1999/xlink">

<homepage

xlink:type="simple" xlink:href="http://www.um.edu.mt">

University of Malta

</homepage>

</homepages>

X Link Syntax

Page 6: 1 Dr Alexiei Dingli XML Technologies X-Languages

6

• Declare the namespace in the top element<homepages xmlns:xlink="http://www.w3.org/1999/xlink">

• Define a simple element i.e. Go from the current position to that address. Multidirectional are not supported by most browsers.

xlink:type="simple" xlink:href="http://www.um.edu.mt"

• To define whether to use the current window or a new one ...

xlink:show="new"

X Link Explanation

Page 7: 1 Dr Alexiei Dingli XML Technologies X-Languages

7

• XLink gets more interesting when we want to access remote locations as resources, instead of standalone pages

• The value of the xlink:show attribute could have been set to "embed". This means that the resource should be processed inline within the page

• With XLink, you can also specify WHEN the resource should appear– xlink:actuate="onLoad" specifies that the resource should be loaded and

shown when the document loads

– xlink:actuate="onRequest" means that the resource is not read or shown before the link is clicked. This is very handy for low-bandwidth settings

Advanced X Links

Page 8: 1 Dr Alexiei Dingli XML Technologies X-Languages

8

• XML Pointer Language

• Allow hyperlinks to point to specific parts of an XML document

• Uses XPath for navigation

X Pointer

Page 9: 1 Dr Alexiei Dingli XML Technologies X-Languages

9

• If a hyperlink points to an XML document we can add an XPointer after the URL

href="http://www.example.com/cdlist.xml#id('rock').child(5)"

• Find the element with unique Id rock

• Point to the 5th item in the list

X Pointer Example

Page 10: 1 Dr Alexiei Dingli XML Technologies X-Languages

10

href="http://www.example.com/cdlist.xml#id('rock')

Is equivalent to

href="http://www.example.com/cdlist.xml#rock

X Pointer Short Hand

Page 11: 1 Dr Alexiei Dingli XML Technologies X-Languages

11

• Lies at the heart of the XML technologies

• Is a syntax for defining parts of document

• Uses path expressions to navigate

• Contains a library of standard function (over 100)

• Major element of XSLT

X Path

Page 12: 1 Dr Alexiei Dingli XML Technologies X-Languages

12

• Element

• Attribute

• Text

• Namespace

• Processing-instruction (<?xml version="1.0"?>)

• Comment

• Root node

X Path Nodes

Page 13: 1 Dr Alexiei Dingli XML Technologies X-Languages

13

• Parent

• Children

• Siblings

• Ancestors

• Descendants

Node relationships

Page 14: 1 Dr Alexiei Dingli XML Technologies X-Languages

14

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>

<title lang="eng">Harry Potter</title>

<price>29.99</price>

</book>

<book>

<title lang="eng">Learning XML</title>

<price>39.95</price>

</book>

</bookstore>

Example

Page 15: 1 Dr Alexiei Dingli XML Technologies X-Languages

15

Expression Description

nodename Selects all child nodes of the named node

/ Selects from the root node

// Selects nodes in the document from the current node that match the selection no matter where they are

. Selects the current node

.. Selects the parent of the current node

@ Selects attributes

Usage

Page 16: 1 Dr Alexiei Dingli XML Technologies X-Languages

16

Path Expression Result

bookstore Selects all the child nodes of the bookstore element

/bookstore Selects the root element bookstoreNote: If the path starts with a slash ( / ) it always represents an absolute path to an element!

bookstore/book Selects all book elements that are children of bookstore

//book Selects all book elements no matter where they are in the document

bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element

//@lang Selects all attributes that are named lang

Examples

Page 17: 1 Dr Alexiei Dingli XML Technologies X-Languages

17

Path Expression Result

/bookstore/book[1] Selects the first book element that is the child of the bookstore element.Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!

/bookstore/book[last()] Selects the last book element that is the child of the bookstore element

/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element

/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element

//title[@lang] Selects all the title elements that have an attribute named lang

//title[@lang='eng'] Selects all the title elements that have an attribute named lang with a value of 'eng'

/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00

/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

Example predicates

Page 18: 1 Dr Alexiei Dingli XML Technologies X-Languages

18

Wildcard Description

* Matches any element node

@* Matches any attribute node

node() Matches any node of any kind

Using Wildcards

Path Expression Result

/bookstore/* Selects all the child nodes of the bookstore element

//* Selects all elements in the document

//title[@*] Selects all title elements which have any attribute

Page 19: 1 Dr Alexiei Dingli XML Technologies X-Languages

19

Path Expression Result

//book/title | //book/price Selects all the title AND price elements of all book elements

//title | //price Selects all the title AND price elements in the document

/bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document

Selecting several paths

Page 20: 1 Dr Alexiei Dingli XML Technologies X-Languages

20

AxisName Result

ancestor Selects all ancestors (parent, grandparent, etc.) of the current node

ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself

attribute Selects all attributes of the current node

child Selects all children of the current node

descendant Selects all descendants (children, grandchildren, etc.) of the current node

descendant-or-self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself

following Selects everything in the document after the closing tag of the current node

following-sibling Selects all siblings after the current node

namespace Selects all namespace nodes of the current node

parent Selects the parent of the current node

preceding Selects everything in the document that is before the start tag of the current node

preceding-sibling Selects all siblings before the current node

self Selects the current node

Axes

Page 21: 1 Dr Alexiei Dingli XML Technologies X-Languages

21

• an axis (defines the tree-relationship between the selected nodes and the current node)

• a node-test (identifies a node within an axis)

• zero or more predicates (to further refine the selected node-set)

axisname::nodetest[predicate]

Location step

Page 22: 1 Dr Alexiei Dingli XML Technologies X-Languages

22

Example Result

child::book Selects all book nodes that are children of the current node

attribute::lang Selects the lang attribute of the current node

child::* Selects all children of the current node

attribute::* Selects all attributes of the current node

child::text() Selects all text child nodes of the current node

child::node() Selects all child nodes of the current node

descendant::book Selects all book descendants of the current node

ancestor::book Selects all book ancestors of the current node

ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is a book node

child::*/child::price Selects all price grandchildren of the current node

Location step example

Page 23: 1 Dr Alexiei Dingli XML Technologies X-Languages

23

An absolute location path:

/step/step/...

A relative location path:

step/step/...

The difference is the initial /

Absolute vrs Relative

Page 24: 1 Dr Alexiei Dingli XML Technologies X-Languages

24

Operator Description Example Return value

| Computes two node-sets //book | //cd Returns a node-set with all book and cd elements

+ Addition 6 + 4 10

- Subtraction 6 - 4 2

* Multiplication 6 * 4 24

div Division 8 div 4 2

= Equal price=9.80 true if price is 9.80false if price is 9.90

!= Not equal price!=9.80 true if price is 9.90false if price is 9.80

< Less than price<9.80 true if price is 9.00false if price is 9.80

<= Less than or equal to price<=9.80 true if price is 9.00false if price is 9.90

> Greater than price>9.80 true if price is 9.90false if price is 9.80

>= Greater than or equal to price>=9.80 true if price is 9.90false if price is 9.70

or or price=9.80 or price=9.70 true if price is 9.80false if price is 9.50

and and price>9.00 and price<9.90 true if price is 9.80false if price is 8.50

mod Modulus (division remainder) 5 mod 2 1

Page 25: 1 Dr Alexiei Dingli XML Technologies X-Languages

25

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async=false;

xmlDoc.load("books.xml");

xpath="/bookstore/book[1]/title";

xmlDoc.selectNodes(xpath);

Programming example

Page 26: 1 Dr Alexiei Dingli XML Technologies X-Languages

26

• Designed to query XML Data

• Like SQL for databases

• Built on XPath

X Query

Page 27: 1 Dr Alexiei Dingli XML Technologies X-Languages

27

doc("books.xml")/bookstore/book/title

Load the document and use XPath to get the data

For more powerful queries use FLWOR expressions

XQuery Example

Page 28: 1 Dr Alexiei Dingli XML Technologies X-Languages

28

• Similar to HTML forms but defined in XML

• Richer and more flexible

• Platform and device independent

• Standard in XHTML 2.0

X Forms

Page 29: 1 Dr Alexiei Dingli XML Technologies X-Languages

29

X Forms Example

Page 30: 1 Dr Alexiei Dingli XML Technologies X-Languages

30

• The form

• The XML

• The Text

X Forms Result

Page 31: 1 Dr Alexiei Dingli XML Technologies X-Languages

31

• XML Stylesheet Language

• Consist of three parts– XSLT– Xpath– XSL-FO

XSL

Page 32: 1 Dr Alexiei Dingli XML Technologies X-Languages

32

• XSL Transformations

• Transforms XML to another XML document

• Uses XPath to navigate

• Most important part of XSL

• Supported by most browsers

XSLT

Page 33: 1 Dr Alexiei Dingli XML Technologies X-Languages

33

• Add

• Remove

• Sort

• Rearrange

• Perform Tests

• Make decisions

• Hide

• Etc ...

Power

Page 34: 1 Dr Alexiei Dingli XML Technologies X-Languages

34

• Use XPath to define elements of source

• When a match occurs– Transform the matching part into the resulting

document

Workings

Page 35: 1 Dr Alexiei Dingli XML Technologies X-Languages

35

CD Catalogue (XML)

CD Catalogue (XSL)

CD Catalogue (XML + XSL)

Combined by adding the following line in the XML document<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>

Example

Page 36: 1 Dr Alexiei Dingli XML Technologies X-Languages

36

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Declaration part of a Stylesheet

Page 37: 1 Dr Alexiei Dingli XML Technologies X-Languages

37

• XSL style sheet made up of different rules called templates

• The Match attribute is used to associate a template to an element in the XML document

<xsl:template>

Page 38: 1 Dr Alexiei Dingli XML Technologies X-Languages

38

Simple Template 1

Page 39: 1 Dr Alexiei Dingli XML Technologies X-Languages

39

Simple Template 1 Result

Page 40: 1 Dr Alexiei Dingli XML Technologies X-Languages

40

Simple Template 2

Page 41: 1 Dr Alexiei Dingli XML Technologies X-Languages

41

Simple Template 2 Result

Page 42: 1 Dr Alexiei Dingli XML Technologies X-Languages

42

Simple Template 3

Page 43: 1 Dr Alexiei Dingli XML Technologies X-Languages

43

Simple Template 3 Result

Page 44: 1 Dr Alexiei Dingli XML Technologies X-Languages

44

Simple Template 4

Page 45: 1 Dr Alexiei Dingli XML Technologies X-Languages

45

Simple Template 4 Result

Page 46: 1 Dr Alexiei Dingli XML Technologies X-Languages

46

Simple Template 5

Page 47: 1 Dr Alexiei Dingli XML Technologies X-Languages

47

Simple Template 5 Result

Page 48: 1 Dr Alexiei Dingli XML Technologies X-Languages

48

Simple Template 6

Page 49: 1 Dr Alexiei Dingli XML Technologies X-Languages

49

Simple Template 6 Result

Page 50: 1 Dr Alexiei Dingli XML Technologies X-Languages

50

Simple Template 7

Page 51: 1 Dr Alexiei Dingli XML Technologies X-Languages

51

Simple Template 7 Result

Page 52: 1 Dr Alexiei Dingli XML Technologies X-Languages

52

Simple Template 8

Page 53: 1 Dr Alexiei Dingli XML Technologies X-Languages

53

Simple Template 8 Result

Page 54: 1 Dr Alexiei Dingli XML Technologies X-Languages

54

• Script to transform using Java Script and XSLT

From the Client Side

Page 55: 1 Dr Alexiei Dingli XML Technologies X-Languages

55

<%

'Load XML

set xml = Server.CreateObject("Microsoft.XMLDOM")

xml.async = false xml.load(Server.MapPath("cdcatalog.xml"))

'Load XSL

set xsl = Server.CreateObject("Microsoft.XMLDOM")

xsl.async = false xsl.load(Server.MapPath("cdcatalog.xsl"))

'Transform file

Response.Write(xml.transformNode(xsl))

%>

From the Server Side (ASP)

Page 56: 1 Dr Alexiei Dingli XML Technologies X-Languages

56

• Language for formatting XML objects

• Can output to screen, paper and other media

• XML files with output information (layout and content)

• Normally stored in .fo, .fob or even .xml

Formatting Objects

Page 57: 1 Dr Alexiei Dingli XML Technologies X-Languages

57

Example FO

Page 58: 1 Dr Alexiei Dingli XML Technologies X-Languages

58

< Root

^ Namespace

< One or more page template

< Page

content

Explanation

Page 59: 1 Dr Alexiei Dingli XML Technologies X-Languages

59

• Pages (Contain many regions)

• Regions (Contain Block Areas)

• Block Areas (Contain Line Areas)

• Line Areas (Contain Inline Areas)

• Inline Areas

Areas

Page 60: 1 Dr Alexiei Dingli XML Technologies X-Languages

60

• region-body (the body of the page)

• region-before (the header of the page)

• region-after (the footer of the page)

• region-start (the left sidebar)

• region-end (the right sidebar)

Regions

Page 61: 1 Dr Alexiei Dingli XML Technologies X-Languages

61

Page Regions

Page 62: 1 Dr Alexiei Dingli XML Technologies X-Languages

62

Page Example

Page 63: 1 Dr Alexiei Dingli XML Technologies X-Languages

63

• Blocks flow into pages

Flow

Page 64: 1 Dr Alexiei Dingli XML Technologies X-Languages

64

• xsl-region-body (into the region-body)

• xsl-region-before (into the region-before)

• xsl-region-after (into the region-after)

• xsl-region-start (into the region-start)

• xsl-region-end (into the region-end)

Where to flow?

Page 65: 1 Dr Alexiei Dingli XML Technologies X-Languages

65

Block Area

Page 66: 1 Dr Alexiei Dingli XML Technologies X-Languages

66

<fo:page-sequence>

<fo:flow flow-name="xsl-region-body"> <fo:block>

<!-- Output goes here -->

</fo:block>

</fo:flow>

</fo:page-sequence>

Block Example

Page 67: 1 Dr Alexiei Dingli XML Technologies X-Languages

67

Block Attributes (1)• Block Margin

– margin

– margin-top

– margin-bottom

– margin-left

– margin-right

• Border style attributes

– border-style

– border-before-style

– border-after-style

– border-start-style

– border-end-style

– border-top-style (same as border-before)

– border-bottom-style (same as border-after)

– border-left-style (same as border-start)

– border-right-style (same as border-end)

• Border color attributes– border-color– border-before-color– border-after-color– border-start-color– border-end-color– border-top-color (same as border-before)– border-bottom-color (same as border-after)– border-left-color (same as border-start)– border-right-color (same as border-end)

• Border width attributes– border-width– border-before-width– border-after-width– border-start-width– border-end-width– border-top-width (same as border-before)– border-bottom-width (same as border-after)– border-left-width (same as border-start)– border-right-width (same as border-end)

Page 68: 1 Dr Alexiei Dingli XML Technologies X-Languages

68

Block Attributes (2)• Block Padding

– padding– padding-before– padding-after– padding-start– padding-end– padding-top (same as padding-before)– padding-bottom (same as padding-after)– padding-left (same as padding-start)– padding-right (same as padding-end)

• Block Background– background-color– background-image– background-repeat – background-attachment (scroll or fixed)

• Font attributes– font-family– font-weight– font-style– font-size– font-variant

• Text attributes– text-align– text-align-last– text-indent– start-indent– end-indent – wrap-option (defines word wrap)– break-before (defines page breaks)– break-after (defines page breaks)– reference-orientation (defines text rotation

in 90" increments)

Page 69: 1 Dr Alexiei Dingli XML Technologies X-Languages

69

Attribute Example

Page 70: 1 Dr Alexiei Dingli XML Technologies X-Languages

70

Attribute Result

Page 71: 1 Dr Alexiei Dingli XML Technologies X-Languages

71

Lists

Page 72: 1 Dr Alexiei Dingli XML Technologies X-Languages

72

Tables

Page 73: 1 Dr Alexiei Dingli XML Technologies X-Languages

73

XSLT + FOC

on

ten

tC

on

ten

tT

ran

sfo

rmin

g u

sin

g X

SL

T+

FO

Tra

nsf

orm

ing

usi

ng

XS

LT

+F

O

Page 74: 1 Dr Alexiei Dingli XML Technologies X-Languages

74

• To use a .fo to generate a document in any other format use:

– Apache FO processor

Software Hint

Page 75: 1 Dr Alexiei Dingli XML Technologies X-Languages

75

Questions?