More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University...
-
date post
21-Dec-2015 -
Category
Documents
-
view
223 -
download
2
Transcript of More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University...
![Page 1: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/1.jpg)
More XMLXML schema, XPATH, XSLT
CS 431 – February 21, 2005Carl Lagoze – Cornell University
acknowledgements tohttp://www.w3schools.com/schema/default.asp
![Page 2: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/2.jpg)
xHTML
• HTML “expressed” in XML• Corrects defects in HTML
– All tags closed– Proper nesting– Case sensitive (all tags lower case)– Strict well-formedness
• Defined by a DTD– <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
– <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
![Page 3: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/3.jpg)
xHTML (cont.)
• All new HTML SHOULD be xHTML• W3C validator
– http://validator.w3.org
• Tidy– http://sourceforge.net/projects/jtidy
![Page 4: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/4.jpg)
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
OWLConceptBuilding
![Page 5: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/5.jpg)
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 6: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/6.jpg)
Structure of a schema
• well-formed xml document• elements are in schema namespace• root is <schema> element
![Page 7: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/7.jpg)
Instantiation of a schema
• Note namespaces!!
![Page 8: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/8.jpg)
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 9: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/9.jpg)
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 10: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/10.jpg)
Facets
• Restrictions on values within type context• Examples
![Page 11: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/11.jpg)
String types and patterns
![Page 12: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/12.jpg)
Simple Example
• Memo Schema – http://www.cs.cornell.edu/lagoze/courses/
CS431/2005sp/Examples/Lecture9/memo.xsd
• Instance Document – http://www.cs.cornell.edu/lagoze/courses/
CS431/2005sp/Examples/Lecture9/memo.xml
![Page 13: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/13.jpg)
Complex Types
• Type definition defines elements nesting
![Page 14: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/14.jpg)
Controls on complex types
• sequence – specific order• all – any order• choice – only one
• cardinality – minOccurs, maxOccurs
![Page 15: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/15.jpg)
Complex Type Extension
• Add values to sequence
![Page 16: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/16.jpg)
Mixed Content
![Page 17: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/17.jpg)
Declaring attributes
• Define type– xs:string – xs:decimal – xs:integer – xs:boolean – xs:date – xs:time
• Define optional or required
![Page 18: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/18.jpg)
Use of attributes
• Always a complex type
![Page 19: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/19.jpg)
Type Reuse
![Page 20: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/20.jpg)
Type Reuse Example
• Address schema– http://www.cs.cornell.edu/lagoze/courses/
CS431/2005sp/Examples/Lecture9/address.xsd
• Person schema– http://www.cs.cornell.edu/lagoze/courses/
CS431/2005sp/Examples/Lecture9/person.xsd
• Instance document– http://www.cs.cornell.edu/lagoze/courses/
CS431/2005sp/Examples/Lecture9/person.xml
![Page 21: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/21.jpg)
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 22: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/22.jpg)
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 23: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/23.jpg)
Axis
• child: all children of context• descendent: all children, grandchildren, …• parent: • ancestor
![Page 24: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/24.jpg)
Node Test
• Element name: e.g. “Book”• Wildcard: *• Type(): where type is “node”, “text”, etc.
![Page 25: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/25.jpg)
Predicate
• Boolean and comparative operators• Types
– Numbers– Strings– node-sets
• Functions– Examples
• boolean starts-with(string, string)• number count(node-set)
![Page 26: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/26.jpg)
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 27: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/27.jpg)
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 28: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/28.jpg)
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 29: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/29.jpg)
XSLT Capabilities
• Generate constant text• Filter out content• Change tree ordering• Duplicate nodes• Sort nodes• Any computational task (XSLT is “turing
complete”)
![Page 30: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/30.jpg)
XSLT Processing Model
Input XMLdoc
Parsedtree
Xformedtree
Outputdoc
(xml, html, etc)
parse XSLT serialize
![Page 31: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/31.jpg)
XSLT “engine”
XMLinput
XSLT“program”
XSLTEngine
(SAXON)
OutputDocument
(xml, html, …)
![Page 32: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/32.jpg)
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 33: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/33.jpg)
Template Form
• Elements from xsl namespace are transform instructions
• Match attribute value is xpath expression
• Non-xsl namespace elements are literals.
![Page 34: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/34.jpg)
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 35: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/35.jpg)
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 36: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/36.jpg)
XSLT Procedural Programming
• Sequential programming style• Basics
– for-each – loop through a set of elements– call-template – like a standard procedure call
![Page 37: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/37.jpg)
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/De
mos/xslt/foreach.xsl
![Page 38: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/38.jpg)
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/De
mos/xslt/call.xsl
![Page 39: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/39.jpg)
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 40: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/40.jpg)
Various other programming constructs
• Conditionals• Variables (declaration and use)• Some type conversion• Sorting
![Page 41: More XML XML schema, XPATH, XSLT CS 431 – February 21, 2005 Carl Lagoze – Cornell University acknowledgements to .](https://reader035.fdocuments.in/reader035/viewer/2022062407/56649d5a5503460f94a3a427/html5/thumbnails/41.jpg)
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/