Title: More XML XML schema, XPATH, XSLT
1More XMLXML schema, XPATH, XSLT
- CS 431 20040225
- Carl Lagoze Cornell University
2A little context
DTD Tag Sets
3XPath
- Language for addressing parts of an XML document
- XSLT
- Xpointer
- Tree model similar to DOM
- W3C Recommendation (1999)
- http//www.w3.org/TR/xpath
4Xpath 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
5Axis
- child all children of context
- descendent all children, grandchildren,
- parent
- ancestor
6Node Test
- Element name e.g. Book
- Wildcard
- Type() where type is node, text, etc.
7Predicate
- Boolean and comparative operators
- Types
- Numbers
- Strings
- node-sets
- Functions
- Examples
- boolean starts-with(string, string)
- number count(node-set)
8Combining all into a location set specification
- Syntax axisnode-testpredicate
- Examples
- childBookposition() lt 3 first three ltBookgt
child elements of context - childBook/attributecolor color attributes
of ltBookgt child elements of context
9Abbreviations
- Child axis is default
- childBook ? Book
- Attribute axis ? _at_
- Bookposition() 1/_at_color
- . (self), .. (parent), //
(descendent-or-self) - position() n ? n
- Example
- Book2/_at_color
10XML 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
11XSLT Capabilities
- Generate constant text
- Filter out content
- Change tree ordering
- Duplicate nodes
- Sort nodes
- Any computational task (XSLT is turing complete)
12XSLT Processing Model
XSLT
serialize
parse
Input XMLdoc
Parsedtree
Xformedtree
Outputdoc(xml, html, etc)
13XSLT engine
XML input
XSLT Engine (SAXON)
Output Document (xml, html, )
XSLT program
14Stylesheet Document or Program
- XML document rooted in ltstylesheetgt 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
15Template Form
- Elements from xsl namespace are transform
instructions - Match attribute value is xpath expression
- Non-xsl namespace elements are literals.
16A simple example
- XSLT file
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xslt/simple.xsl - XML base file
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xslt/simple.xml
17XSLT Recursive Programming Style
- Document driven, template matching
- Conflict resolution rules
- Mode setting
- ltxslapply-templates modethisgt
- ltxsltemplate matchfoo modethisgt
- ltxsltemplate matchfoo modethatgt
- Context setting
- ltxslapply-templates select//bargt
18XSLT Procedural Programming
- Sequential programming style
- Basics
- for-each loop through a set of elements
- call-template like a standard procedure call
19For-each programming example
- XSLT file
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xslt/foreach.xsl - XML base file
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xslt/foreach.xml
20Call-template programming example
- XML base file
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xslt/call.xml - XSLT file
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xslt/call.xsl
21Result Tree Creation
- Literals any element not in xsl namespace
- ltxsltextgt - content directly to output
- ltxslvalue-ofgt - expression processing
- ltxslcopygt and ltxslcopyofgt - Copy current node
or selected nodes into result tree - ltxslelementgt - instantiate an element
- ltxslattributegt - instantiate an attribute
22Various other programming constructs
- Conditionals
- Variables (declaration and use)
- Some type conversion
- Sorting
23Resources
- XSLT WROX Press
- ISBN 1-861005-06-7
- W3C XSLT Page
- http//www.w3.org/Style/XSL/
- TopXML XSL Tutorial
- http//www.vbxml.com/xsl/tutorials/intro/default.a
sp
24XML 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
25Simple vs. Complex Values
- Element with complex value contains other
elements (has children) - Element with simple value does not have children
(e.g. text).
26Simple Value Types
- Restriction on type of content
- Syntax
- ltxselement namexxx typeyyy/gt
- Examples
- ltxselement namelastname typexsstring/gt
- ltxselement nameage typexsnumber/gt
- ltxselement nameage typexsdate/gt
27Facets
- Restrictions on values within type context
- Examples
28Simple Example
- Memo Schema
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xmlschema/memo.xsd - Memo Instance Document
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xmlschema/memo.xml
29Type extension/restriction
30Type Reuse
31Type Reuse Example
- Base Type Definition
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xmlschema/address.xsd - Type Use
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xmlschema/person.xsd - Instance Document
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xmlschema/person.xml
32DTD XML Schema Equivalence
- DTD Example
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xmlschema/channel.dtd - Equivalent Schema
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xmlschema/channel.xsd - Instance Document
- http//www.cs.cornell.edu/Courses/cs502/2002SP/Dem
os/xmlschema/channel.xml