Ertan Deniz Instructor
XML Schema Document Navigation (Xpath) Document Transformation (XSLT)
Portions of this lecture are derived from in “Web Services: Principles and Technology” by Michael P. Papazoglou and its accompanying instructors materials, © Pearson Education Limited 2008.
Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008 XML schema refers to a document that defines the content of and structure for expressing XML documents. Schema is commonly used in the area of databases to refer to the logical structure of a database. Schemas provide support for additional meta-data characteristics such as structural relationships, cardinality, valid values, and data types. Each type of schema acts as a method of describing data characteristics and applying rules and constraints to a referencing XML document. An XML schema describes the elements and attributes that may be contained in a schema conforming document and the ways that the elements may be arranged within a document structure.
Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008 Sophie Jones 34 File “Person.xml” <xs:element name="Last“ type="xs:string"/> File “Person.xsd”
Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008 Annotation (Schema Comment) Element Name, Type Simple Type (Int, String) Complex Type MinOccurs,MaxOccurs Attribute Name,Type Sequence (Specifies the exact Order) Choice ( Any one, but not at all) All (All required, any order) Extension,Restriction
Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008 One item of a purchase order with its details Needs to be specified in US$ Elements and their content modelElements and their content model
Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008 Attributesand Attribute Groups Attributes and Attribute Groups
Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008
< xsd:schema xmlns:xsd=" xmlns:PO=" targetNamespace=" Michael P. Papazoglo u, Web Services, 1 st Edition, © Pearson Education Limited 2008
Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008 <xsd:element name="shippingAddress" type="PO:Address" minOccurs= "1" maxOccurs="1"/> <xsd:element name="billingAddress" type="PO:Address" minOccurs= "1" maxOccurs="1"/> </xsd:complexType Variant of the PurchaseOrder type that uses the base type Address for its billingAddress and shippingAddress elements.
Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008 Plastic Products 459 Wickham st. Fortitude Valley QLD 4006 Australia 158 Edward st. QLD 4000 Australia An instance document can now use any type derived from base type Address for its billingAddress and shippingAddress elements. PurchaseOrder type uses the derived AustralianPostalAddress type as its billingAddress and the derived AustralianAddress type as its shippingAddress elements.
XML Schemas Document Navigation (Xpath) Document Transformation (XSLT)
XPath is a standard for developing expressions that can be used to find specific pieces of information within an XML document. XPath is an abstract language that defines a tree model that codifies the logical structure of an XML document against which all expressions are evaluated. selects nodes in an XML document Michael P. Papazoglou, Web Services, 1 st Edition, © Pearson Education Limited 2008
Kenneth M.Anderson, CSCI 7818 Lecture
Example Xpath gradebook/student/grade is an XPath expression that selects all “grade” nodes in the example on the previous slide XPath can even select attributes More Examples //grade “start at the root node and find all grade nodes” gradebook/student[2] “select the second student node under gradebook” Kenneth M.Anderson, CSCI 7818 Lecture
XML Schema Document Navigation (Xpath) Document Transformation (XSLT)
XSLT stands for XML Stylesheet Language, Transformations XML is primarily used to describe and contain data. Although the most obvious and effective use of XML is to describe data, the eXtensible Stylesheet Language Transform (XSLT) can also be used to format or transform XML content for presentation to users. The XSLT process transforms an XML structure into presentation technology such as HTML or into any other required forms and structures. XSLT uses the XML Path Language (Xpath) to address and locate sections of XML documents. Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008
XSLT was developed as part of the XML stylesheet standards effort What’s a stylesheet? A stylesheet is a device for specifying presentation information independent of content Stylesheets in HTML The Web already has a stylesheet language called “cascading stylesheets” or CSS Specifies mechanisms for transforming XML to other structures XML->XML XML->HTML XML->PDF Kenneth M.Anderson, CSCI 7818 Lecture
To understand XSLT, you must view XML documents as tree structures XSLT provides rules to transform one tree into another tree It traverses the source tree in an order dictated by the stylesheet and generates the destination tree using the rules of the stylesheet Kenneth M.Anderson, CSCI 7818 Lecture
XSLT transforms XML documents using stylesheets that are themselves XML documents All XSLT stylesheets have the following form <xsl:stylesheet version="1.0" xmlns:xsl=" …templates and transformation rules go here… Kenneth M.Anderson, CSCI 7818 Lecture
Stylesheets consist of templates that “match” nodes of the source XML tree (i.e. document) Each template then specifies what should be generated in the destination tree (or document) Match="/" Associates the template with the root of the XML source document A template looks like this: Grade Book Kenneth M.Anderson, CSCI 7818 Lecture
Stylesheet processing XSLT processor is handed a document and a stylesheet It starts a (breadth-first) traversal at the root node and checks to see if there is a template match If so, it applies the template and looks for an “xsl:apply-templates” element If such an element exists, it continues the traversal if no such element exists, the traversal stops If not, it traverses down the tree looking for a template match with some other node of the tree Kenneth M.Anderson, CSCI 7818 Lecture
The apply-templates tag determines if an XSLT processor continues traversing a document once a template match has occurred The apply-templates tag can contain an attribute called “select” which can specify the specific children to continue traversing using an XPath expression All children traversed All grade nodes with a name attribute equal to “HW4” traversed (any other nodes skipped during the subsequent traversal) Kenneth M.Anderson, CSCI 7818 Lecture
Do something here... The select attribute is an Xpath expression that selects the nodes to iterate over Kenneth M.Anderson, CSCI 7818 Lecture
Grade Book Grade: Kenneth M.Anderson, CSCI 7818 Lecture
Grade Book Grade: 10 Grade: 7 Grade: 6 Grade: 10 … more grades here... Kenneth M.Anderson, CSCI 7818 Lecture Grade Book Grade: 10 Grade: 7 Grade: 6 Grade: 10 HTML Result
Kenneth M.Anderson, CSCI 7818 Lecture <xsl:when test = “position()=last()”> Do something for last element <xsl:when test = “position()=first()”> Do something for first element Do something for other elements
Kenneth M.Anderson, CSCI 7818 Lecture This selects all student nodes, sorts them by name, and then applies templates to them
We can not cover entire standards Xpath Standard XSLT Standard Reference XSLT General Online XSLT Sample atalog&xsltfile=cdcatalog atalog&xsltfile=cdcatalog Reference on XSLT html Kenneth M.Anderson, CSCI 7818 Lecture
Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited (Book Slides) Kenneth M.Anderson, CSCI 7818 – XSLT Overview overview.pdf overview.pdf XSLT ve Xpath resources .NET XML Tools Tutorial .NET Xpath Sample (Code Project)
Thanks for your interest...