Computer Science E-259 XML with Java

Lecture 4: XPath 1.0 (and 2.0) and XSLT 1.0 (and 2.0) 15 October 2007 David J. Malan [email protected]

1 Copyright © 2007, David J. Malan . All Rights Reserved.

Computer Science E-259 Last Time ƒ ƒ ƒ

DOM Level 3 JAXP 1.3 and Xerces 2.7.1 My First XML Parser

2 Copyright © 2007, David J. Malan . All Rights Reserved.

Last Time DOM Level 3 Document

Comment Jim Bob graduate Text: \n\t

Text: \n\t\t

Element: name

Text: Jim Bob

Element: students

Element: student

Text: \n\t\t

Attr: id

Element: status

Text: \n

Text: \n\t

Text: graduate

3 Copyright © 2007, David J. Malan . All Rights Reserved.

Last Time JAXP 1.3 and Xerces 2.7.1 javax.xml.parsers.DocumentBuilderFactory javax.xml.parsers.DocumentBuilder org.w3c.dom.* ...

4 Copyright © 2007, David J. Malan . All Rights Reserved.

Last Time My First XML Parser cscie259.project1.mf.*

5 Copyright © 2007, David J. Malan . All Rights Reserved.

Computer Science E-259 This Time ƒ ƒ ƒ ƒ ƒ

CSS Level 2 XPath 1.0 (and 2.0) XSLT 1.0 (and 2.0) TrAX Project 2

6 Copyright © 2007, David J. Malan . All Rights Reserved.

CSS Level 2 By Example

7 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) History ƒ ƒ

XML Path Language (XPath) Version 1.0 is a Recommendation since 11/99 XML Path Language (XPath) Version 2.0 is a Recommendation since 1/07

8 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) Location Paths

/child::movies/child::movie[@rating='R']

step

axis

node test

predicate

location path

9 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) Axes ƒ ƒ ƒ ƒ ƒ ƒ ƒ ƒ ƒ

ancestor, ancestor-or-self attribute child descendant, descendant-or-self following, following-sibling namespace parent preceding, preceding-sibling self

10 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) following-sibling

1

2

11 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) descendant

1

3

2

4

5

6

12 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) ancestor 2

1

13 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) ...

14 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) Node Tests ƒ ƒ ƒ ƒ ƒ ƒ ƒ ƒ

foo foo:bar foo:* * node() comment() text() processing-instruction()

15 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) Abbreviated Synatx ƒ ƒ ƒ ƒ ƒ

child:: attribute:: /descendant-or-self::node()/ self::node() parent::node()

≡ ≡ ≡ ≡ ≡

'' @ // . ..

16 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) Data Types ƒ ƒ ƒ ƒ ƒ

boolean number string node-set external object

17 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) boolean ƒ ƒ ƒ ƒ

true(), false() =, !=, <, >, <=, >= and, or not()

18 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) number ƒ ƒ ƒ ƒ

=, !=, <, >, <=, >= +, -, *, div, mod, floor(), ceiling() ...

19 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) string ƒ ƒ

ƒ

"foo", 'foo' concat(), contains(), starts-with(), string-length(), substring(), substring-after(), substring-before(), translate() ...

20 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) node set ƒ ƒ ƒ

count(), current(), last(), name(), position(), sum() | ...

21 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) Converting Types ƒ

Explicit Conversion ƒ boolean(), string(), number()

ƒ

Implicit Conversion ƒ false » 0, true » 1 ƒ false » 'false', true » 'true' ƒ 0 » false, other » true ƒ '' » false, other » true ƒ empty » false, other » true ƒ ...

22 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Motivation ƒ ƒ ƒ

XML provides a syntax for structured data formats No one format is likely to enable all possible uses for data Transforming XML can be useful in two different scenarios ƒ Data conversion: transforming one format to another ƒ Multiple data formats for B2B purchase orders ƒ Different description formats for family trees ƒ Publishing: transforming data to a human viewable form ƒ Displaying XML data on the Web as HTML ƒ Displaying XML data in print using PDF

23 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) History ƒ ƒ

XSL Transformations (XSLT) Version 1.0 is a Recommendation since 11/99 (first draft dates back to 8/98) XSL Transformations (XSLT) Version 2.0 is a Recommendation since 1/07

24 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) XHTML 1.0 My First XSLT-Generated Webpage

25 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Nodes ƒ ƒ ƒ ƒ ƒ ƒ ƒ

root node element nodes attribute nodes text nodes comment nodes PI nodes ...

26 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Elements ƒ ƒ ƒ ƒ ƒ

xsl:stylesheet xsl:apply-templates, xsl:call-template, xsl:template, xsl:with-param xsl:for-each xsl:value-of ...

27 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Matching Templates ... ...

28 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Named Templates ... ...

29 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Applying Templates

30 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Built-In Templates

31 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Values of Nodes

32 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Recursive Descent Processing ƒ ƒ ƒ

ƒ

Start from the root node Find a matching template Instantiate the body of the template ƒ Send literal result elements (i.e., non-XSLT elements) to standard output ƒ Interpret XSLT elements as instructions In other words, selects nodes to which the processor recursively applies the same algorithm: match templates and instantiate their bodies

33 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Processors ƒ

Xalan-J 2.7.0 ƒ

ƒ ƒ ƒ ƒ ƒ

java org.apache.xalan.xslt.Process -IN foo.xml -XSL foo.xsl

Microsoft Core XML Services (MSXML) 6.0 SAXON 8.9 Stylus Studio 2007 XML Enterprise Suite XMLSpy 2008 Enterprise Edition ...

34 Copyright © 2007, David J. Malan . All Rights Reserved.

XSLT 1.0 (and 2.0) Versus CSS Level 2 Why two stylesheet languages?

35 Copyright © 2007, David J. Malan . All Rights Reserved.

TrAX APIs javax.xml.transform.* javax.xml.transform.stream.* javax.xml.transform.dom.* javax.xml.transform.sax.*

36 Copyright © 2007, David J. Malan . All Rights Reserved.

Project 2 Overview ƒ ƒ ƒ

It's B2B Time! My Blockbuster XTube

37 Copyright © 2007, David J. Malan . All Rights Reserved.

Next Time XPath 1.0 (and 2.0) and XSLT 1.0 (and 2.0), Continued ƒ ƒ

XPath 1.0 (and 2.0), Continued XSLT 1.0 (and 2.0), Continued

38 Copyright © 2007, David J. Malan . All Rights Reserved.

Computer Science E-259 XML with Java

Lecture 4: XPath 1.0 (and 2.0) and XSLT 1.0 (and 2.0) 15 October 2007 David J. Malan [email protected]

39 Copyright © 2007, David J. Malan . All Rights Reserved.

XPath 1.0 (and 2.0) - Fas Harvard

Computer Science E-259. This Time. ▫ CSS Level 2. ▫ XPath 1.0 (and 2.0). ▫ XSLT 1.0 (and 2.0). ▫ TrAX. ▫ Project 2 .... Displaying XML data on the Web as HTML.

83KB Sizes 1 Downloads 216 Views

Recommend Documents

No documents