32
Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

More XMLXML schema, XPATH, XSLT

CS 502 – 20020214Carl Lagoze – Cornell University

Page 2: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

A little context

Traditional LibraryCentral control

Uniform expertise

Traditional WebDistributed, interlinkedViewable Documents

XMLMarkup Syntax

URIsName Convention

HTTPAccess Method

SchemaType Definition

NamespacesConcept

Integration

XpathData

Decomposition

XSLTData

Transformation

DTDTag Sets

RDFSemantic

Relationships

DAML+OILConceptBuilding

Page 3: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

XML Schema Define…

• elements• attributes• Nesting structure (parent/child rela.) • Sibling sequence • Sibling cardinality • Presence or absence of text values • Element and attribute data types • Element and attribute default values

Page 4: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Simple vs. Complex Values

• Element with complex value contains other elements (has children)

• Element with simple value does not have children (e.g. text).

Page 5: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Simple Value Types

• Restriction on type of content• Syntax

– <xs:element name=“xxx” type=“yyy”/>

• Examples– <xs:element name=“lastname” type=“xs:string”/>– <xs:element name=“age” type=“xs:number”/>– <xs:element name=“age” type=“xs:date”/>

Page 6: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Facets

• Restrictions on values within type context• Examples

Page 7: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Simple Example

• Memo Schema– http://www.cs.cornell.edu/Courses/cs502/2002SP/De

mos/xmlschema/memo.xsd

• Memo Instance Document– http://www.cs.cornell.edu/Courses/cs502/2002SP/De

mos/xmlschema/memo.xml

Page 8: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Type extension/restriction

Page 9: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Type Reuse

Page 10: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Type Reuse Example

• Base Type Definition– http://www.cs.cornell.edu/Courses/cs502/2002SP/De

mos/xmlschema/address.xsd

• Type Use– http://www.cs.cornell.edu/Courses/cs502/2002SP/De

mos/xmlschema/person.xsd

• Instance Document– http://www.cs.cornell.edu/Courses/cs502/2002SP/De

mos/xmlschema/person.xml

Page 11: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

DTD & XML Schema Equivalence

• DTD Example– http://www.cs.cornell.edu/Courses/cs502/2002SP/

Demos/xmlschema/channel.dtd

• Equivalent Schema– http://www.cs.cornell.edu/Courses/cs502/2002SP/

Demos/xmlschema/channel.xsd

• Instance Document– http://www.cs.cornell.edu/Courses/cs502/2002SP/

Demos/xmlschema/channel.xml

Page 12: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

XPath

• Language for addressing parts of an XML document– XSLT– Xpointer

• Tree model similar to DOM• W3C Recommendation (1999)

– http://www.w3.org/TR/xpath

Page 13: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Xpath Concepts

• Context Node– current node in XML document that is basis of path

evaluation– Default to root

• Location Steps – selection from context node– Axis – sub-tree(s) selection from context node– Node Test – select specific elements or node type(s)– Predicates – predicate for filtering after axis and

node tests

Page 14: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Axis

• child: all children of context• descendent: all children, grandchildren, …• parent: • ancestor

Page 15: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Node Test

• Element name: e.g. “Book”• Wildcard: *• Type(): where type is “node”, “text”, etc.

Page 16: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Predicate

• Boolean and comparative operators• Types

– Numbers– Strings– node-sets

• Functions– Examples

• boolean starts-with(string, string)• number count(node-set)

Page 17: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Combining all into a location set specification

• Syntax: axis::node-test[predicate]• Examples:

– child::Book[position() <= 3] – first three <Book> child elements of context

– child::Book/attribute::color – “color” attributes of <Book> child elements of context

Page 18: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Abbreviations

• Child axis is default– child::Book Book

• Attribute axis @– Book[position() = 1]/@color

• “.” (self), “..” (parent), “//” (descendent-or-self)• position() = n n• Example

– Book[2]/@color

Page 19: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

XML Transformations (XSLT)

• Origins: separate rendering from data– Roots in CSS

• W3C Recommendation– http://www.w3.org/TR/xslt

• Generalized notion of transformation for:– Multiple renderings– Structural transformation between different

languages– Dynamic documents

• XSLT – rule-based (declarative) language for transformations

Page 20: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

XSLT Capabilities

• Generate constant text• Filter out content• Change tree ordering• Duplicate nodes• Sort nodes• Any computational task (XSLT is “turing

complete”)

Page 21: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

XSLT Processing Model

Input XMLdoc

Parsedtree

Xformedtree

Outputdoc

(xml, html, etc)

parse XSLT serialize

Page 22: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

XSLT “engine”

XMLinput

XSLT“program”

XSLTEngine

(SAXON)

OutputDocument

(xml, html, …)

Page 23: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Stylesheet Document or Program

• XML document rooted in <stylesheet> element

• Body is set of templates– Xpath expression specifies elements in source tree– Body of template specifies contribution of source

elements to result tree

• Not sequential execution

Page 24: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Template Form

• Elements from xsl namespace are transform instructions

• Match attribute value is xpath expression

• Non-xsl namespace elements are literals.

Page 25: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

A simple example

• XML base file– http://www.cs.cornell.edu/Courses/cs502/2002SP/De

mos/xslt/simple.xml

• XSLT file– http://www.cs.cornell.edu/Courses/cs502/2002SP/De

mos/xslt/simple.xsl

Page 26: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

XSLT Recursive Programming Style

• Document driven, template matching– Conflict resolution rules– Mode setting

• <xsl:apply-templates mode=“this”>• <xsl:template match=“foo” mode=“this”>• <xsl:template match=“foo” mode=“that”>

– Context setting• <xsl:apply-templates select=“//bar”>

Page 27: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

XSLT Procedural Programming

• Sequential programming style• Basics

– for-each – loop through a set of elements– call-template – like a standard procedure call

Page 28: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

For-each programming example

• XML base file– http://www.cs.cornell.edu/Courses/cs502/2002SP/De

mos/xslt/foreach.xml

• XSLT file– http://www.cs.cornell.edu/Courses/cs502/2002SP/

Demos/xslt/foreach.xsl

Page 29: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Call-template programming example

• XML base file– http://www.cs.cornell.edu/Courses/cs502/2002SP/

Demos/xslt/call.xml

• XSLT file– http://www.cs.cornell.edu/Courses/cs502/2002SP/

Demos/xslt/call.xsl

Page 30: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Result Tree Creation

• Literals – any element not in xsl namespace• <xsl:text> - content directly to output• <xsl:value-of> - expression processing• <xsl:copy> and <xsl:copyof> - Copy current

node or selected nodes into result tree• <xsl:element> - instantiate an element• <xsl:attribute> - instantiate an attribute

Page 31: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Various other programming constructs

• Conditionals• Variables (declaration and use)• Some type conversion• Sorting

Page 32: Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – 20020214 Carl Lagoze – Cornell University

Cornell CS 502

Resources

• XSLT – WROX Press– ISBN 1-861005-06-7

• W3C XSLT Page– http://www.w3.org/Style/XSL/

• Arbortext XSL Tutorial– http://www.nwalsh.com/docs/tutorials/xsl/