Ertan Deniz [email protected] Instructor. XML Schema Document Navigation (Xpath) Document...

34
CENG454 – XML and XML Web Services Ertan Deniz [email protected] Instructor XML Technologies

Transcript of Ertan Deniz [email protected] Instructor. XML Schema Document Navigation (Xpath) Document...

Page 1: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

CENG454 – XML and XML Web Services

Ertan [email protected]

Instructor

XML Technologies

Page 2: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Agenda XML Schema Document Navigation (Xpath) Document Transformation (XSLT)

Page 3: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Main Reference 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.

Page 4: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XML Schema

Michael P. Papazoglou, Web Services, 1st 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.

Page 5: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XML schema and instance document

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008

<?xml version="1.0" encoding="UTF-8"?>

<Person xmlns:xsi="http://www.w3.org/ 2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Person.xsd">

<First>Sophie</First>

<Last>Jones</Last>

<Age>34</Age>

</Person>

File “Person.xml”<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/ XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="First“ type="xs:string"/> <xs:element name="Middle“ type="xs:string“ minOccurs="0"/> <xs:element name="Last“ type="xs:string"/> <xs:element name="Age“ type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>

File “Person.xsd”

Page 6: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XML Schema Content

Michael P. Papazoglou, Web Services, 1st 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

Page 7: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Schema components

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008

<element name="item"><annotation>

<documentation>One item of a purchase order with itsdetails</documentation>

</annotation><complexType>

<sequence><choice>

<element name="productName" type="string"/><element name="productName" type="string"/>

</choice><element name="quantity">

<simpleType><restriction base="positiveInteger">

<maxExclusive value="100"/></restriction>

</simpleType></element><element name="price" type="decimal">

<annotation><documentation>Needs to be specified in

US$</documentation></annotation>

</element><element ref="ipo:comment" minOccurs="0"

maxOccurs="5"/><element name="shipDate" type="date" minOccurs="0"/>

</sequence><attribute name="partNum" type="ipo:Sku"/>

</complexType></element>

• Elements and their content model

Page 8: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Schema components

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008

<xs:attribute name="name" type="xs:NCName"/>

<xs:attribute name="mixed" type="xs:boolean" use="optional" default="false"/>

<xs:attribute name="abstract" type="xs:boolean" use="optional" default="false"/>

<xs:attribute name="final" type="xs:derivationSet"/>

<xs:attribute name="block" type="xs:derivationSet"/>

Attributes and Attribute Groups

Page 9: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Schema components – Complex Types

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008

<xsd:complexType name="PersonType"><xsd:sequence>

<xsd:element name="First" type="xsd:string"/><xsd:element name="Last" type="xsd:string"/><xsd:element name="Title" type="xsd:string“

minOccurs="0"/><xsd:element name="PhoneExt" type="xsd:int"/><xsd:element ref="EMail"/>

</xsd:sequence></xsd:complexType><xsd:element name="Person" type="PersonType"/><xsd:element name="VIP“

substitutionGroup="Person"><xsd:complexType>

<xsd:complexContent><xsd:extension base="PersonType">

<xsd:attribute name="IQ" type="xsd:int"/></xsd:extension>

</xsd:complexContent></xsd:complexType>

</xsd:element>

Page 10: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XML Schema – Complex Type Extensions<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:PO="http://www.plastics_supply.com/PurchaseOrder" targetNamespace="http://www.plastics_supply.com/PurchaseOrder">

<xsd:complexType name="Address"> <xsd:sequence> <xsd:element name="Number" type="xsd:decimal"/> <xsd:element name="Street" type="xsd:string"/> <xsd:element name="City" type="xsd:string" minOccurs="0"/> </xsd:sequence></xsd:complexType>

<xsd:complexType name="AustralianAddress"> <xsd:complexContent>

<xsd:extension base="PO:Address"><xsd:sequence> <xsd:element name="State" type="xsd:string"/> <xsd:element name="PostalCode" type="xsd:decimal"/> <xsd:element name="Country" type="xsd:string"/></xsd:sequence>

</xsd:extension> </xsd:complexContent></xsd:complexType>

</xsd:schema>

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008

Page 11: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XML Schema – Complex Type Restrictions

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008

<xsd:complexType name="AustralianPostalAddress"> <xsd:complexContent> <xsd:restriction base="PO:AustralianAddress">

<xsd:sequence> <xsd:element name="Number" type="xsd:decimal"/> <xsd:element name="Street" type="xsd:string"/> <xsd:element name="City" type="xsd:string" minOccurs="0"

maxOccurs="0"/> <xsd:element name="State" type="xsd:string"/> <xsd:element name="PostalCode" type="xsd:decimal"/>

<xsd:element name="Country" type="xsd:string"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType>

Page 12: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XML Schema – Complex Type Polymorphism

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008

<xsd:complexType name="PurchaseOrder"> <xsd:sequence> <xsd:element name="Name" minOccurs="1" maxOccurs="1"> <xsd:simpleType> <xsd:restriction base="xsd:string"/> </xsd:simpleType> </xsd:element> <xsd:element name="shippingAddress" type="PO:Address" minOccurs= "1" maxOccurs="1"/> <xsd:element name="billingAddress" type="PO:Address" minOccurs= "1" maxOccurs="1"/> <xsd:choice minOccurs="1" maxOccurs="1"> <xsd:element name="BillingDate" type="xsd:date"/> <xsd:element name="ShippingDate" type="xsd:date"/> </xsd:choice> </xsd:sequence> </xsd:complexType Variant of the PurchaseOrder type that uses

the base type Address for its billingAddress and shippingAddress elements.

Page 13: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XML Schema – Complex Type Polymorphism

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008

<?xml version="1.0" encoding=”UTF-8”?><PO:PurchaseOrder xmlns:PO="http://www.plastics_supply.com/PurchaseOrder">

<Name> Plastic Products </Name> <shippingAddress xsi:type="PO:AustralianAddress"> <Number> 459 </Number> <Street> Wickham st. </Street> <City> Fortitude Valley </City> <State> QLD </State> <PostalCode> 4006 </PostalCode> <Country> Australia </country> </shippingAddress> <billingAddress xsi:type=="PO:AustralianAddress"> <Number> 158 </Number> <Street> Edward st. </Street> <State> QLD </State> <PostalCode> 4000 </PostalCode> <Country> Australia </Country> </billingAddress> <BillingDate> 2002-09-15 </BillingDate></PO:PurchaseOrder>

An instance document can now use any type derived from base type Address for its billingAddress and shippingAddress elements.

PurchaseOrder type uses thederived AustralianPostalAddress type as its billingAddress and the derived AustralianAddress type as its shippingAddress elements.

Page 14: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Agenda XML Schemas Document Navigation (Xpath) Document Transformation (XSLT)

Page 15: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Xpath Standart 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, 1st Edition, © Pearson Education Limited 2008

Page 16: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Sample XML View as tree

Kenneth M.Anderson, CSCI 7818 Lecture

Page 17: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Xpath Expressions 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

gradebook/student/grade[@name=“hw3”] 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

Page 18: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Agenda XML Schema Document Navigation (Xpath) Document Transformation (XSLT)

Page 19: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

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

Page 20: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Transformations 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

Page 21: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XSLT 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

Page 22: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XSLT XSLT transforms XML documents using stylesheets that

are themselves XML documents All XSLT stylesheets have the following form

<?xml version="1.0"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">…templates and transformation rules go here…

</xsl:stylesheet>

Kenneth M.Anderson, CSCI 7818 Lecture

Page 23: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Stylesheets 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:

<xsl:template match="/"> <html>

<head><title>Grade Book</title>

</head><xsl:apply-templates/>

</html></xsl:template>

Kenneth M.Anderson, CSCI 7818 Lecture

Page 24: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XSLT Architecture

Kenneth M.Anderson, CSCI 7818 Lecture

Page 25: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

More Details

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

Page 26: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XSL : apply-templates

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 <xsl:apply-templates/>

All children traversed <xsl:apply-templates select=“grade[@name=‘HW4’]”>

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

Page 27: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

XSL : Repetition

<xsl:for-each select = “item”>Do something here ...

</xsl:for-each>

The select attribute is an Xpath expression that selects the nodes to iterate over

Kenneth M.Anderson, CSCI 7818 Lecture

Page 28: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Repetition Example<xsl:template match="/">

<html><head>

<title>Grade Book</title></head><body><ul><xsl:for-each select=“student/grade”><li>Grade: <xsl:value-of select=".”/></li></xsl:for-each></ul></body>

</html></xsl:template>

Kenneth M.Anderson, CSCI 7818 Lecture

Page 29: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Repetition Example (HTML Result)<html><head><title>Grade

Book</title></head><body><ul><li>Grade: 10<li><li>Grade: 7<li><li>Grade: 6<li><li>Grade: 10<li>… more grades here ...</ul></body></html>

Kenneth M.Anderson, CSCI 7818 Lecture

Grade Book Grade: 10 Grade: 7 Grade: 6 Grade: 10

HTML Result

Page 30: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Additional Tags

Kenneth M.Anderson, CSCI 7818 Lecture

<xsl:value-of select=“.”> <xsl:if test=“position()=last()”> <xsl:choose>

<xsl:when test = “position()=last()”> Do something for last element </xsl:when> <xsl:when test = “position()=first()”> Do something for first element </xsl:when> <xsl:otherwise> Do something for other elements </xsl:otherwise></xsl:choose>

Page 31: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Additional Tags

Kenneth M.Anderson, CSCI 7818 Lecture

<xsl:sort data-type=“” select=“” order=“”><xsl:apply-templates select=“//student”>

<xsl:sort select=“name”/> </xsl:apply-templates>

This selects all student nodes, sorts them by name, and then applies templates to them

Page 32: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

More Information

We can not cover entire standards Xpath Standard

http://www.w3.org/TR/xpath XSLT Standard Reference

http://www.w3.org/TR/xslt/ XSLT General

http://www.xslt.com/ Online XSLT Sample http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=

cdcatalog&xsltfile=cdcatalog

Reference on XSLThttp://www.zvon.org/xxl/XSLTreference/Output/index

.html

Kenneth M.Anderson, CSCI 7818 Lecture

Page 33: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

References

Michael P. Papazoglou, Web Services, 1st Edition, © Pearson Education Limited 2008. (Book Slides) Kenneth M.Anderson, CSCI 7818 – XSLT Overview http://www.cs.colorado.edu/~kena/classes/7818/f08/lectures/lecture_2_xslt_overview.pdf XSLT ve Xpath resourceshttp://www.w3schools.com .NET XML Tools Tutorial .NET Xpath Sample (Code Project)

http://www.codeproject.com/KB/XML/gsxpathtutorial.aspx

Page 34: Ertan Deniz Ertan.deniz70@gmail.com Instructor.  XML Schema  Document Navigation (Xpath)  Document Transformation (XSLT)

Questions ?

Thanks for your interest...