XML Query and Transformation XPath - XSLT

Similar documents
Markup Sprachen und semi-strukturierte Daten

XSL - Introduction and guided tour

By Nabil ADOUI, member of the 4D Technical Support team

T XML in 2 lessons! %! " #$& $ "#& ) ' */,: -.,0+(. ". "'- (. 1

Agents and Web Services

Chapter 3: XML Namespaces

XML and Data Management

XML WEB TECHNOLOGIES

Interactive, dynamic Scalable Vector Graphics

10CS73:Web Programming

Interactive Data Visualization for the Web Scott Murray

Presentation / Interface 1.3

XSLT - A Beginner's Glossary

XSLT Mapping in SAP PI 7.1

Computer Science E-259

XSL Elements. xsl:copy-of

XML Processing and Web Services. Chapter 17

IMPLEMENTING AN XML COURSE IN THE COLLEGE OF BUSINESS

WTP-101 Developing Web Applications with Standards. using W3C org standard technologies such as, HTML, CSS, XML, XSD and XSL

aconferenceonxml Lesser Town Campus Prague, Czech Republic June 17-18, 2006

XML for RPG Programmers: An Introduction

Last Week. XML (extensible Markup Language) HTML Deficiencies. XML Advantages. Syntax of XML DHTML. Applets. Modifying DOM Event bubbling

Course Information Course Number: IWT 1229 Course Name: Web Development and Design Foundation

XML and Tools. Muhammad Khalid Sohail Khan Mat #: University of Duisburg Essen Germany

Concrete uses of XML in software development and data analysis.

Markup Languages and Semistructured Data - SS 02

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

High Performance XML Data Retrieval

Introduction to XML Applications

Creating a TEI-Based Website with the exist XML Database

An Approach to Eliminate Semantic Heterogenity Using Ontologies in Enterprise Data Integeration

Web Services Technologies

XPath Processing in a Nutshell

Database & Information Systems Group Prof. Marc H. Scholl. XML & Databases. Tutorial. 11. SQL Compilation, XPath Symmetries

Extending the Linked Data API with RDFa

INTEROPERABILITY BETWEEN VISUAL UML DESIGN APPLICATIONS AND AUTHORING TOOLS FOR LEARNING DESIGN

F O R U M N O K I A. Implementing Trucking Logistics with Nokia Handsets. Version 1.0; July 18, Java /XML

PHP Oracle Web Development Data Processing, Security, Caching, XML, Web Services and AJAX

Exchanger XML Editor - Canonicalization and XML Digital Signatures

Cloud Computing, Interactive Websites, and Scientific Research/Education

CST6445: Web Services Development with Java and XML Lesson 1 Introduction To Web Services Skilltop Technology Limited. All rights reserved.

Semistructured data and XML. Institutt for Informatikk INF Ahmet Soylu

REST vs. SOAP: Making the Right Architectural Decision

WTP-101 Developing Rich Internet Applications with Java Web Frameworks

T Network Application Frameworks and XML Web Services and WSDL Tancred Lindholm

Data Integration through XML/XSLT. Presenter: Xin Gu

Introduction to Ingeniux Forms Builder. 90 minute Course CMSFB-V6 P

IT6503 WEB PROGRAMMING. Unit-I

Lesson 4 Web Service Interface Definition (Part I)

INTRO TO XMLSPY (IXS)

REDUCING THE COST OF GROUND SYSTEM DEVELOPMENT AND MISSION OPERATIONS USING AUTOMATED XML TECHNOLOGIES. Jesse Wright Jet Propulsion Laboratory,

Application development in XML

Mobile Web Design with HTML5, CSS3, JavaScript and JQuery Mobile Training BSP-2256 Length: 5 days Price: $ 2,895.00

Data XML and XQuery A language that can combine and transform data

2009 Martin v. Löwis. Data-centric XML. Other Schema Languages

An XML Based Data Exchange Model for Power System Studies

Invited Expert on XForms and HTML Working Group

WWW. World Wide Web Aka The Internet. dr. C. P. J. Koymans. Informatics Institute Universiteit van Amsterdam. November 30, 2007

Java and XML parsing. EH2745 Lecture #8 Spring

XML. CIS-3152, Spring 2013 Peter C. Chapin

Extending XSLT with Java and C#

What's new in 3.0 (XSLT/XPath/XQuery) (plus XML Schema 1.1) Michael Kay Saxonica

Web Server Logs Analyze Using the XML Technologies

Extracting data from XML. Wednesday DTL

Rigorous and Automatic Testing of Web Applications

WebSphere Business Monitor

Unified XML/relational storage March The IBM approach to unified XML/relational databases

Developing Web Views for VMware vcenter Orchestrator

Web Development CSE2WD Final Examination June (a) Which organisation is primarily responsible for HTML, CSS and DOM standards?

Stylus Studio 2010 XML Feature Comparison Matrix

ART 379 Web Design. HTML, XHTML & CSS: Introduction, 1-2

HETEROGENEOUS DATABASE INTEGRATION FOR WEB APPLICATIONS

Developers Guide. Designs and Layouts HOW TO IMPLEMENT WEBSITE DESIGNS IN DYNAMICWEB. Version: English

DataDirect XQuery Technical Overview

Agenda Summary of Previous Session

David RR Webber Chair OASIS CAM TC (Content Assembly Mechanism)

Change Management for XML, in XML

XML- New meta language in e-business

XML: extensible Markup Language. Anabel Fraga

DTD Tutorial. About the tutorial. Tutorial

Short notes on webpage programming languages

06 XML-based Technologies

Building A Very Simple Website

JÁN LACKO, EUGEN RUŽICKÝ WEB TECHNOLOGIES AND DESIGN

Overview of DatadiagramML

Release 1. ICAPRG604A Create cloud computing services

LabVIEW Internet Toolkit User Guide

Portals and Hosted Files

BASI DI DATI II 2 modulo Parte II: XML e namespaces. Prof. Riccardo Torlone Università Roma Tre

Service Oriented Computing: SOAP, WSDL and UDDI. Dr. Cristian Mateos Diaz ( ISISTAN - CONICET

Enabling Web Services

MarkLogic Server. Application Developer s Guide. MarkLogic 8 February, Last Revised: 8.0-4, November, 2015

Indexing XML Data in RDBMS using ORDPATH

Responsive Web Design Creative License

Design and implementation of a mobile database for Java phones

CSET 3100 Advanced Website Design (3 semester credit hours) IT Required

CIS 467/602-01: Data Visualization

How To Use X Query For Data Collection

Facebook Twitter YouTube Google Plus Website . o Zooming and Panning. Panel. 3D commands. o Working with Canvas

Developer Tutorial Version 1. 0 February 2015

Transcription:

XML Query and Transformation XPath - XSLT Prof. Cesare Pautasso http://www.pautasso.info cesare.pautasso@unisi.ch 1

XML Technology Landscape Data Representation XML Syntax XML Information Set (InfoSet) XML Namespaces XML Schema, DTD XLink, XPointer Data Processing XPath XSLT Extensible Stylesheet Transformation Language XQuery XUpdate Data Processing API DOM SAX JAXP Communication Protocols XML Forms XML Web Services (SOAP, WSDL, UDDI) XML Encryption XML Digital Signature 2

XPath Contents XML and CSS XSLT Extensible Stylesheet Language Transformation 3

XPath 4

XPath Overview /library/book/title XPath Expression Input XML Document Tree XPath Processor XPath Result 5

XPath L XPath specifies a navigation path along the XML tree in order to identify a subset of the nodes that are reachable using such path /library/book/title <library> <book isbn= 0321269667 /> <book> <author>a</author> <title>xml for dummies</title> <edition>10 th, <year>2008</year></edition> </book> </library> B B A T E Y 6

Location Paths A Location Path is a sequence of Location Steps (separated by /) Location Steps are used to test whether a node should be traversed axis :: nodetest [ expression ] Select the navigation direction: child (default) attribute self parent descendant descendant-or-self ancestor following-sibling... Check whether the node matches: name * *:localname prefix:* Match nodes based on their kind: text() comment() node() 7

Root Element Working with Trees 8

Working with Trees Leaves 9

Working with Trees Self 10

Working with Trees Parent 11

Working with Trees Ancestors 12

Working with Trees Children 13

Descendents Working with Trees 14

Working with Trees Preceding-Sibling Following-Sibling 15

Working with Trees Preceding 16

Working with Trees Following 17

XPath by Example Select All Book Titles /books/book/title/text() //title/text() Return the ISBN of the books /books/book/@isbn Return the year of the books that have a publisher /books/book/publisher/../year Count how many book elements are in the document count(//books) 18

Compact Notation Default axis (Child) /child::books/child::book /books/book Attribute axis replaced by @ //book/attribute::isbn //book/@isbn Navigating to the parent node shortened like in the file system publisher/parent::node()/child::year publisher/../year Match any subtree /descendant-or-self:node()/author //author 19

XPath Predicate Examples Select the 2 nd Book /books/book[2] Return the author of the books that have an ISBN /books/book[@isbn]/author Return the books that have a single author /books/book[count(author)=1] Sum the prices (in EUR) of all books sum(//price[@currency="eur"]) Select all books at even positions //book[position() mod 2 = 0] 20

XPath 1.0 Functions Node Set Functions last position count id name Math Functions sum floor ceiling round String Manipulation Functions concat starts-with contains normalize-space string-length substring translate 21

XPath and JavaScript function runxpath(xml, xpath) { } //setup an empty document tree xmldom=document.implementation.createdocument("","",null); xmldom.onload = function() { //run the xpath query var nodes=xmldom.evaluate(xpath, xmldom, null, XPathResult.ANY_TYPE, null); //process the result nodes } //load and parse the XML document into the dom xmldom.load(xml); 22

XSLT extensible Stylesheet Language Transformation 23

Format XML with CSS Add this processing instruction to your XML document and open it in a browser: <?xml-stylesheet type="text/css" href= style.css"?> The CSS style sheet can use the same formatting properties as for HTML pages. The only difference are the selectors, which should refer to the XML element tags as defined by the DTD/Schema of the XML document Limitations: Only XML text elements are visualized (attributes remain hidden) Cannot sort, manipulate and filter the information of the XML Cannot introduce additional HTML page elements (images, tables, forms ) Solution: use XSLT instead. <?xml-stylesheet type="text/xsl href="style.xsl"?> 24

XSLT Overview XSLT Document Input XML Document Tree XSLT Processor Output XML Document Tree http://saxon.sourceforge.net/ http://eclipsexslt.sourceforge.net/ 25

XSLT extensible Stylesheet Language Transformation (W3C, 1.0 1999 2.0 2007) XSLT transforms an XML document into another XML document (so the output can also be in XHTML) XSLT is a declarative language based on rules that match elements of the input XML document and transform them based on templates. XSLT uses a simplified version of XPath to navigate and access the input XML document nodes XSLT uses Namespaces to mix XSLT processing tags with the XML templates that specify the structure of the output document 26

XSLT Example Template Rule <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0 xmlns:xsl="http://www.w3.org/1999/xsl/transform"> XPath Expressions <xsl:template match="/books"> <html><body> <h2>my Library</h2> <table border="1"> <tr> <th>isbn</th> <th>author</th> <th>title</th> </tr> <xsl:for-each select="book[@isbn]"> <tr> <td class="isbn"><xsl:value-of select="@isbn" /></td> <td><xsl:value-of select="author" /></td> <td><xsl:value-of select="title" /></td> </tr> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet> 27

XSLT Template <xsl:template match="pattern"> Output </xsl:template> Example Patterns Name Name/Name Name[@a= ] / * Name Name The Template element defines the structure of the Output that is produced when the Pattern rule (XPath) matches the Input document If more than one pattern matches, the most specific is executed (like CSS selectivity) The template contains a set of constructor elements instructions and XML elements that will be literally copied in the output Templates can be associated with Modes (2.0) 28

Reading the input <xsl:value-of select="pattern"/> {Pattern} The value-of instruction is replaced with the value of the XPath pattern evaluated in the context of the input elements matched by the template The {} notation is only used to generate attribute values <xsl:for-each select="pattern"> Iterate over all elements returned by the XPath pattern <xsl:apply-templates select="pattern"> <xsl:call-template name=" "> Call another template rule (Helps to modularize the stylesheet) 29

Generating the output Apart from literal values, you can use explicit instructions to construct the output document: <xsl:element name= > <xsl:attribute name= select= Pattern /> <xsl:text> </xsl:text> </xsl:element> These are more verbose, but can be useful if you do not want to mix the XSLT instructions with the literal elements of the output. <xsl:text>2+2 = </xsl:text> <xsl:value-of select="2+2"/> 30

Explicit Constructors Example <xsl:for-each select="book"> <xsl:element name="tr"> <xsl:element name="td"> <xsl:attribute name="class" select=" isbn "> <xsl:value-of select="@isbn" /> </xsl:element> <xsl:element name="td"> <xsl:value-of select="author" /> </xsl:element> <xsl:element name="td"> <xsl:value-of select="title" /> </xsl:element> </xsl:element> </xsl:for-each> 31

Variables and Parameters <xsl:template name="fib"> <xsl:param name="n"/> <xsl:choose> <xsl:when test="$n le 1"> <xsl:value-of select="1"/> </xsl:when> <xsl:otherwise> <xsl:variable name="f1"> <xsl:call-template name="fib"> <xsl:with-param name="n" select="$n - 1"/> </xsl:call-template> </xsl:variable> <xsl:variable name="f2"> <xsl:call-template name="fib"> <xsl:with-param name="n" select="$n - 2"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$f1 + $f2"/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="/"> <xsl:call-template name="fib"> <xsl:with-param name="n select="10"/> </xsl:call-template> </xsl:template> Templates (and entire stylesheets) can be parametric. Two forms of variable declaration: <xsl:variable name= select= Pattern /> <xsl:variable name= > Value </xsl:variable> 32

XSLT Applications XSLT XHTML Input XML Document Tree XSLT Processor SVG XSL-FO Apache FOP PDF 33

References Anders Moller and Michael Schwartzbach, An Introduction to XML and Web Technologies, Addison-Wesley, 2006 Elliotte Rusty Harold and W. Scott Means, XML in a Nutshell, O Reilly, 3 rd Ed. 2004 Sal Mangano, XSLT Cookbook, O Reilly, 2005 34