+ All Categories
Home > Documents > XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to...

XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to...

Date post: 27-Dec-2015
Category:
Upload: camilla-jefferson
View: 227 times
Download: 1 times
Share this document with a friend
Popular Tags:
33
XML and XPath
Transcript
Page 1: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

XML and XPath

Page 2: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 2

EXtensible Markup Language (XML)

a W3C standard to complement HTML A markup language much like HTML origins: structured text SGML motivation:

HTML describes presentationXML describes content

HTML XML SGML

http://www.w3.org/TR/2000/REC-xml-20001006 (version 2, 10/2000)

Page 3: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 3

HTML Describes the Presentation

Page 4: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 4

HTML Describes the Presentation

<h1>Bibliography</h1><p><i>Foundations of Databases</i> Abiteboul, Hull, Vianu <br>Addison Wesley, 1995<p><i>Data on the Web</i> Abiteoul, Buneman, Suciu <br>Morgan Kaufmann, 1999

Page 5: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 5

XML Describes the Contents

<bibliography><book><title> Foundations… </title>

<author> Abiteboul </author><author> Hull </author><author> Vianu </author><publisher> Addison Wesley </publisher><year> 1995 </year>

</book>…

</bibliography>

Page 6: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 6

More on XML

XML was designed to describe data contents XML tags are not predefined in XML.

You must “invent” your own tags XML uses a Document Type Definition (DTD) or

an XML Schema to describe the data XML with a DTD or XML Schema is designed

to be self-descriptive

Page 7: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 7

Applications and Impact of XML

Data can be stored outside HTML Data can be exchanged between incompatible

systems Financial information can be exchanged over the

Internet Plain text files can be used to share data Data is available to more users Used to create new languages

WML, WSDL, BPEL, ……

Page 8: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 8

Benefits of Using XML

XML is structured XML documents are easily committed to a

persistence layer XML is platform independent, textual information XML is an open standard XML is language independent DOM and SAX are open, language-independent

set of interfaces XML is web enabled XML is totally extensible XML supports shareable structure (using DTDs) XML enables interoperability

Page 9: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 9

XML Terminology

tags: book, title, author, … start tag: <book>, end tag: </book> elements: <book>…</book>,

<author>…</author> elements are nested empty element: <red></red> abbrv. <red/> an XML document: single root element

well formed XML document: if it has matching tags

Page 10: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 10

Attributes are Alternative Ways to Represent Data

<book price = “55” currency = “USD”> <title>Foundations of Databases</title> <author>Abiteboul</author> … <year> 1995 </year></book>

Page 11: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 11

CDATA Section

Syntax: <![CDATA[ .....any text here...]]>

<script><![CDATA[function matchwo (a, b){if (a < b && a < 0) then

{return 1}else{return 0}

}]]></script>

Page 12: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 12

Entity References

Syntax: &entityname; Example:

<element> this is less than &lt; </element> Some entities:

&lt; <

&gt; >

&amp; &

&apos; ‘

&quot; “

&#38;Unicode

char

Page 13: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 13

Processing Instructions

Syntax: <?target argument?> Example:

<product><name> Alarm Clock </name> <?ringBell 20?> <price>19.99</price></product>

What do they mean ?

Page 14: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 14

XML Comments

Syntax <!-- .... comment text... -->

Yes, they are part of the data model !!!

Page 15: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 15

Name Conflicts

Solution: prefix

<table><tr><td>Apples</td><td>Bananas</td></tr>

</table>

<table><name>African Coffee

Table</name>

<width>80</width><length>120</length>

</table>

<h:table><h:tr><h:td>Apples</

h:td><h:td>Bananas</

h:td></h:tr>

</h:table>

<f:table><f:name>African Coffee

Table</f:name><f:width>80</f:width><f:length>120</f:length>

</f:table>

Page 16: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 16

<tag xmlns:mystyle = “http: // …”>…<mystyle:title> … </mystyle:title><mystyle:number> …

</tag>

XML Namespaces

syntactic: <number> , <isbn:number> semantic: provide URL for schema

Page 17: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 17

XML Namespaces

http://www.w3.org/TR/REC-xml-names (1/99) name ::= [prefix:]localpart

<book xmlns:isbn“www.isbn-org.org/def”> <title> … </title> <number> 15 </number> <isbn:number> … </isbn:number></book>

Page 18: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 18

http://www.w3.org/TR/xpath (11/99) Building block for other W3C standards:

XSL Transformations (XSLT) XML Link (XLink)XML Pointer (XPointer)XML Query

Was originally part of XSL

XPath

Page 19: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 19

Example for XPath Queries<bib>

<book> <publisher> Addison-Wesley </publisher> <author> Serge Abiteboul </author> <author> <first-name> Rick </first-name> <last-name> Hull </last-name></author> <author> Victor Vianu </author> <title> Foundations of Databases </title> <year> 1995 </year>

</book><book price=“55”>

<publisher> Freeman </publisher> <author> Jeffrey D. Ullman </author> <title> Principles of Database and Knowledge Base Systems </title> <year> 1998 </year>

</book></bib>

Page 20: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 20

Data Model for XPath

bib

book book

publisher author . . . .

Addison-Wesley Serge Abiteboul

The root

The root elementProcessing instruction

Comment

Page 21: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 21

Simple Expressions

/bib/book/year

Result: <year> 1995 </year> <year> 1998 </year>

/bib/paper/year

Result: empty (there were no papers)

Page 22: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 22

Restricted Kleene Closure

//author

Result: <author> Serge Abiteboul </author> <author><first-name> Rick </first-name>

<last-name> Hull </last-name> </author> <author> Victor Vianu </author> <author> Jeffrey D. Ullman </author>

/bib//first-name

Result: <first-name> Rick </first-name>

Page 23: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 23

Functions

/bib/book/author/text()

Result: Serge Abiteboul Victor Vianu Jeffrey D. UllmanRick Hull doesn’t appear because he has firstname,

lastname

Functions in XPath:text() = matches the text valuenode() = matches any node (= * or @* or

text())name() = returns the name of the current tag

Page 24: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 24

Wildcard

//author/

Result: <first-name> Rick </first-name>

<last-name> Hull </last-name>

Matches any element

Page 25: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 25

Attribute Nodes

/bib/book/@price

Result: “55”

@price means that price is has to be an attribute

Page 26: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 26

Qualifiers

/bib/book/author[first-name]

Result: <author><first-name> Rick </first-name> <last-name> Hull </last-name> </author>

/bib/book/author[firstname][address[//zip][city]]/lastname

Result: <lastname> … </lastname> <lastname> … </lastname>

Page 27: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 27

More Qualifiers

/bib/book[@price < “60”]

/bib/book[author/@age < “25”]

/bib/book[author/text()]

Page 28: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 28

XPath Summary

bib matches a bib element matches any element/ matches the root element/bib matches a bib element under

rootbib/paper matches a paper in bibbib//paper matches a paper in bib, at any

depth//paper matches a paper at any depthpaper|book matches a paper or a book@price matches a price attributebib/book/@price matches price attribute in book,

in bibbib/book[@price<“55”]/author/lastname

matches…

Page 29: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 29

XPath: More Details

An XPath expression, p, establishes a relation between:A context node, andA node in the answer set

In other words, p denotes a function:S[p] : Nodes -> {Nodes}

Examples:author/firstname. = self.. = parentpart///subpart/../name =

part//[subpart]/name

Page 30: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 30

The Root

<bib><paper>1</paper><paper>2</paper></bib>

bib is the document element The root is above bib

/bib = returns the document element / = returns the root

Why? Because we may have comments before and after <bib>; they become siblings of <bib>

This is advanced xmlogy

Page 31: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 31

XPath Navigation We can navigate along 13 axes:

ancestorancestor-or-selfattributechilddescendantdescendant-or-selffollowingfollowing-siblingnamespaceparentprecedingpreceding-siblingself

We’ve only seen these, so far

Page 32: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 32

Examples

Examples:child::author/child::lastname =

author/lastnamechild::author/descendant::zip = author//zipchild::author/parent:: = author/..child::author/attribute::age = author/@age

What does this mean ?paper/publisher/parent::/author/bib//address[ancestor::book]/bib//author/ancestor:://zip

Page 33: XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.

Web Services: XML+XPath 33

More Examples

name() = the name of the current node/bib//[name()=book] same as /bib//book

What does this mean ?/bib//[ancestor::[name()!=book]]

Navigation axis gives us strictly more power !


Recommended