XML. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

Similar documents
Querying XML. COSC 304 Introduction to Database Systems. XML Querying. Example DTD. Example XML Document. Path Descriptions in XPath

XML. COSC Dr. Ramon Lawrence. An attribute is a name-value pair declared inside an element. Comments. Page 3. COSC Dr.

XML. Objectives. Duration. Audience. Pre-Requisites

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 27-1

7.1 Introduction. extensible Markup Language Developed from SGML A meta-markup language Deficiencies of HTML and SGML

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 7 XML

XML in Databases. Albrecht Schmidt. al. Albrecht Schmidt, Aalborg University 1

XSLT program. XSLT elements. XSLT example. An XSLT program is an XML document containing

Part 2: XML and Data Management Chapter 6: Overview of XML

Java EE 7: Back-end Server Application Development 4-2

CSI 3140 WWW Structures, Techniques and Standards. Representing Web Data: XML

Data Presentation and Markup Languages

EMERGING TECHNOLOGIES. XML Documents and Schemas for XML documents

XML: Extensible Markup Language

Introduction. " Documents have tags giving extra information about sections of the document

XML: Introduction. !important Declaration... 9:11 #FIXED... 7:5 #IMPLIED... 7:5 #REQUIRED... Directive... 9:11

EXAM IN SEMI-STRUCTURED DATA Study Code Student Id Family Name First Name

Querying XML Data. Querying XML has two components. Selecting data. Construct output, or transform data

EXAM IN SEMI-STRUCTURED DATA Study Code Student Id Family Name First Name

Delivery Options: Attend face-to-face in the classroom or via remote-live attendance.

Introduction. " Documents have tags giving extra information about sections of the document

EXAM IN SEMI-STRUCTURED DATA Study Code Student Id Family Name First Name

Introduction to Semistructured Data and XML. Overview. How the Web is Today. Based on slides by Dan Suciu University of Washington

Introduction to XML. XML: basic elements

Chapter 1: Getting Started. You will learn:

Delivery Options: Attend face-to-face in the classroom or remote-live attendance.

Introduction Syntax and Usage XML Databases Java Tutorial XML. November 5, 2008 XML

The concept of DTD. DTD(Document Type Definition) Why we need DTD

Chapter 13 XML: Extensible Markup Language

Dr. Awad Khalil. Computer Science & Engineering department

Gestão e Tratamento de Informação

XSLT. Announcements (October 24) XSLT. CPS 116 Introduction to Database Systems. Homework #3 due next Tuesday Project milestone #2 due November 9

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

~ Ian Hunneybell: DIA Revision Notes ~

11. EXTENSIBLE MARKUP LANGUAGE (XML)

COMP9321 Web Application Engineering

Structured documents

XML Origin and Usages

What is XML? XML is designed to transport and store data.

XML. Jonathan Geisler. April 18, 2008

Session [2] Information Modeling with XSD and DTD

Extreme Java G Session 3 - Sub-Topic 5 XML Information Rendering. Dr. Jean-Claude Franchitti

UNIT 3 XML DATABASES

PART. Oracle and the XML Standards

The main Topics in this lecture are:

XML and XSLT. XML and XSLT 10 February

CountryData Technologies for Data Exchange. Introduction to XML

CLASS DISCUSSION AND NOTES

The Extensible Markup Language (XML) and Java technology are natural partners in helping developers exchange data and programs across the Internet.

XPath and XQuery. Introduction to Databases CompSci 316 Fall 2018

Introduction p. 1 An XML Primer p. 5 History of XML p. 6 Benefits of XML p. 11 Components of XML p. 12 BNF Grammar p. 14 Prolog p. 15 Elements p.

.. Cal Poly CPE/CSC 366: Database Modeling, Design and Implementation Alexander Dekhtyar..

Web Programming Paper Solution (Chapter wise)

Querying and Transforming XML Data Chapter5 Contents

Introduction to XSLT. Version 1.0 July nikos dimitrakas

COPYRIGHTED MATERIAL. Contents. Part I: Introduction 1. Chapter 1: What Is XML? 3. Chapter 2: Well-Formed XML 23. Acknowledgments

Introduction to XML. Chapter 133

Author: Irena Holubová Lecturer: Martin Svoboda

XML: Managing with the Java Platform

XML Extensible Markup Language

XML, DTD, and XPath. Announcements. From HTML to XML (extensible Markup Language) CPS 116 Introduction to Database Systems. Midterm has been graded

Introduction to XML (Extensible Markup Language)

DBMaker. XML Tool User's Guide

Semistructured Data and XML

XSLT: How Do We Use It?

Contents. 1 Introduction Basic XML concepts Historical perspectives Query languages Contents... 2

Web Services Week 3. Fall Emrullah SONUÇ. Department of Computer Engineering Karabuk University

XML. Marie Dubremetz Uppsala, April 2014

WHITE PAPER. Query XML Data Directly from SQL Server Abstract. DilMad Enterprises, Inc. Whitepaper Page 1 of 32

IBM. XML and Related Technologies Dumps Braindumps Real Questions Practice Test dumps free

HTML vs. XML In the case of HTML, browsers have been taught how to ignore invalid HTML such as the <mymadeuptag> element and generally do their best

SDPL : XML Basics 2. SDPL : XML Basics 1. SDPL : XML Basics 4. SDPL : XML Basics 3. SDPL : XML Basics 5

Text Properties and Languages

COMP9321 Web Application Engineering

XML. Structure of XML Data XML Document Schema Querying and Transformation Application Program Interfaces to XML Storage of XML Data XML Applications

W3C XML XML Overview

XML databases. Jan Chomicki. University at Buffalo. Jan Chomicki (University at Buffalo) XML databases 1 / 9

XML. extensible Markup Language. Overview. Overview. Overview XML Components Document Type Definition (DTD) Attributes and Tags An XML schema

markup language carry data define your own tags self-descriptive W3C Recommendation

XML. extensible Markup Language. ... and its usefulness for linguists

Data Exchange. Hyper-Text Markup Language. Contents: HTML Sample. HTML Motivation. Cascading Style Sheets (CSS) Problems w/html

extensible Markup Language

MASTER OF SCIENCE THESIS

Extensible Markup Language (XML) Hamid Zarrabi-Zadeh Web Programming Fall 2013

Exam : Title : XML 1.1 and Related Technologies. Version : DEMO

XML for Java Developers G Session 3 - Main Theme XML Information Modeling (Part I) Dr. Jean-Claude Franchitti

Semistructured data, XML, DTDs

Style Sheet A. Bellaachia Page: 22

One of the main selling points of a database engine is the ability to make declarative queries---like SQL---that specify what should be done while

XML for Java Developers G Session 3 - Main Theme XML Information Modeling (Part I) Dr. Jean-Claude Franchitti

Introduction to XML. An Example XML Document. The following is a very simple XML document.

Semantic Web. XSLT: XML Transformation. Morteza Amini. Sharif University of Technology Fall 95-96

XPath and XSLT. Overview. Context. Context The Basics of XPath. XPath and XSLT. Nodes Axes Expressions. Stylesheet templates Transformations

XML Data Management. 5. Extracting Data from XML: XPath

Presentation 19: XML technologies part 2: XSL, XSLT, XSL-FO, XPath & XML Programming

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML

Introduction to XML. M2 MIA, Grenoble Université. François Faure

Introduction to XSLT. Version 1.3 March nikos dimitrakas

XML. Document Type Definitions. Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Transcription:

XML Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

XML INTRODUCTION 2

THE XML LANGUAGE XML: Extensible Markup Language Standard for the presentation and transmission of information. An XML file is a text organized using tags. Similar to HTML But here tags structure content not the way it s displayed. Tree shaped. Specially apt for streaming. This unit contains some samples and supplementary material. These can be found also as separate files so they can be played with. The slide will make a reference to them when appropriate. 3

XML RULES Tag: defines a piece of content. There are opening and a closing tag. Begin with < and ends with >. Closing tags begin with </. Block: content enclosed by a tag. Example: <title>my title</title> Attribute: extra information provided by a tag. Inside the opening tag. Surrounded by double quotes ( ). Attributes must have a value. Example: <book price="100">harry Potter</book> The closing tag is optional: only the opening tag. Opening tag ends with />. Have no block inside. Example: <remark text="good" /> Comments: Not processed. Between <!--" and -->. XML is case-sensitive. Spaces after the first are ignored. 4

XML EXAMPLE <Bookstore> <!-- This is a bookstore --> <Book ISBN="ISBN-0-13-713526-2" Price="85" Edition="3rd"> <Title>A First Course in Database Systems</Title> <Authors> <Author> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> <Author> <First_Name>Jennifer</First_Name> <Last_Name>Widom</Last_Name> </Author> </Authors> <Import /> </Book> <Book ISBN="ISBN-0-13-815504-6" Price="100"> <Title>Database Systems: The Complete Book</Title> <Remark>Buy this book bundled with "A First Course"!</Remark> <Authors><Author> <First_Name>Hector</First_Name> <Last_Name>Garcia</Last_Name> </Author> </Authors> </Book> </Bookstore> 5

RELATIONAL MODEL VS. XML Relational model Structure: Tables Schema: Predetermined Queries: Easy and intuitive Ordering: None Best for: Machines, storage XML Structure: Tree hierarchy Schema: Flexible, self-descriptive Queries: A little more complex Ordering: Implicit Best for: Humans, streaming 6

WELL-FORMED XML An XML document is well-formed if it complies with some basic structural requirements: Only one root element. Opening and closing tags are matched. Proper nesting. Uniquely-names attributes in each element. Parsers: DOM, SAX XML Document XML Parser Parsed XML Error! 7

SHOWING XML Rule-based languages can be used to transform XML into HTML: CSS: Cascading Style Sheets. XSL: extensible Stylesheet Language. The XML is processed by the parser first. Parsed XML CSS / XSL Interpreter HTML Document Rules 8

XML STANDARDS XML is the most popular data representation and exchange format. There are plenty of standards that work alongside XML. These are the most important: DTD XSD XPath XQuery XSL 9

DTD AND XML SCHEMA 10

VALID XML An XML document is well-formed if it complies with some basic structural requirements: Only one root element. Opening and closing tags are matched. Proper nesting. Uniquely-named attributes in each element. An XML document is valid if it complies with some content-specific requirements. These requirements can be specified using two standards: DTD: Document Type Descriptor. XSD: XML Schema. This section uses the supplementary material marked as 01-DTD XSD. 11

VALIDATING XML DTD / XSD XML Document XML Parser with validation Parsed XML Not well-formed! Not valid! 12

DTD Document Type Descriptor. Simple standard to validate XML. Provides a grammar that can be used to define: Elements Attributes Nesting Ordering Number of occurrences DTD has some special attributes (untyped pointers): ID IDREF / IDREFS The easiest way to learn DTD is by example! 13

SAMPLE DTD <!DOCTYPE Bookstore [ <!ELEMENT Bookstore (Book Magazine)*> <!ELEMENT Book (Title, Authors, Remark?)> <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED Edition CDATA #IMPLIED> <!ELEMENT Magazine (Title)> <!ATTLIST Magazine Month CDATA #REQUIRED Year CDATA #REQUIRED> <!ELEMENT Title (#PCDATA)> <!ELEMENT Authors (Author+)> <!ELEMENT Remark (#PCDATA)> <!ELEMENT Author (First_Name, Last_Name)> <!ELEMENT First_Name (#PCDATA)> <!ELEMENT Last_Name (#PCDATA)> ]> 14

XML FOR SAMPLE DTD <Bookstore> <Book ISBN="ISBN-0-13-713526-2" Price=" 100" Edition="3rd"> <Title>A First Course in Database Systems</Title> <Authors> <Author> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> <Author> <First_Name>Jennifer</First_Name> <Last_Name>Widom</Last_Name> </Author> </Authors> </Book> <Book ISBN="ISBN-0-13-815504-6" Price="100"> <Title>Database Systems: The Complete Book</Title> <Authors> <Author> <First_Name>Hector</First_Name> <Last_Name>Garcia-Molina</Last_Name> </Author> <Author> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> <Author> <First_Name>Jennifer</First_Name> <Last_Name>Widom</Last_Name> </Author> </Authors> <Remark> Buy this book bundled with "A First Course" - a great deal! </Remark> </Book> </Bookstore> 15

SAMPLE DTD WITH POINTERS <!DOCTYPE Bookstore [ <!ELEMENT Bookstore (Book*, Author*)> <!ELEMENT Book (Title, Remark?)> <!ATTLIST Book ISBN ID #REQUIRED Price CDATA #REQUIRED Authors IDREFS #REQUIRED> <!ELEMENT Title (#PCDATA)> <!ELEMENT Remark (#PCDATA BookRef)*> <!ELEMENT BookRef EMPTY> <!ATTLIST BookRef book IDREF #REQUIRED> <!ELEMENT Author (First_Name, Last_Name)> <!ATTLIST Author Ident ID #REQUIRED> <!ELEMENT First_Name (#PCDATA)> <!ELEMENT Last_Name (#PCDATA)> ]> 16

XML FOR SAMPLE DTD WITH POINTERS <Bookstore> <Book ISBN="ISBN-0-13-713526-2" Price="100" Authors="JU JW"> <Title>A First Course in Database Systems</Title> </Book> <Book ISBN="ISBN-0-13-815504-6" Price="85" Authors="HG JU JW"> <Title>Database Systems: The Complete Book</Title> <Remark> Amazon.com says: Buy this book bundled with <BookRef book="isbn-0-13-713526-2" /> - a great deal! </Remark> </Book> <Author Ident="HG"> <First_Name>Hector</First_Name> <Last_Name>Garcia-Molina</Last_Name> </Author> <Author Ident="JU"> <First_Name>Jeffrey</First_Name> <Last_Name>Ullman</Last_Name> </Author> <Author Ident="JW"> <First_Name>Jennifer</First_Name> <Last_Name>Widom</Last_Name> </Author> </Bookstore> 17

XSD XML Schema. Very broad standard to validate XML. Provides a grammar that can be used to define: Elements Attributes Nesting Ordering Number of occurrences Data types Keys Pointers (typed) XSD is out of the scope of this unit. We just provide an example so you can recognize it when you see it. 18

SAMPLE XSD <xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema"> <xsd:element name="bookstore"> <xsd:complextype> <xsd:sequence> <xsd:element name="book" type="booktype minoccurs="0" maxoccurs="unbounded" /> <xsd:element name="author" type="authortype" minoccurs="0" maxoccurs="unbounded" /> </xsd:sequence> </xsd:complextype> <xsd:key name="bookkey"> <xsd:selector xpath="book" /><xsd:field xpath="@isbn" /> </xsd:key> <xsd:key name="authorkey"> <xsd:selector xpath="author" /><xsd:field xpath="@ident" /> </xsd:key> <xsd:keyref name="authorkeyref" refer="authorkey"> <xsd:selector xpath="book/authors/auth" /><xsd:field xpath="@authident" /> </xsd:keyref> <xsd:keyref name="bookkeyref refer="bookkey"> <xsd:selector xpath="book/remark/bookref" /><xsd:field xpath="@book" /> </xsd:keyref> </xsd:element> 19

ADVANTAGES AND DISADVANTAGES OF USING DTD/XSD Advantages Applications can assume that there s an underlying structure to the document. CSS/XML can be used safely. It s easier to write documentation when there s a fixed structure. All other advantages of strong typing. Disadvantages A well-formed XML is easier to modify and more flexible. DTD/XSD files can end becoming too big and cumbersome. All other disadvantages of weak typing. 20

XPATH AND XQUERY 21

NAVIGATING AN XML We must think of an XML file as a tree. XPath is an standard that allows us to navigate such tree. XPath takes an XML document or stream as input. This section uses the supplementary material marked as 02-XPath, which contains a sample XML and several examples of how to use XPath. Running these examples in a tool like Kernow is the best way to learn. Basic XPath building blocks: /: Separator //: Me and all my children /TagName: Tags /@AttributeName: Atributtes To get its value we write: /data(@attributename) : OR, used with parenthesis *: Wildcard 22

XPATH CONDITIONS Conditions are used to filter tags: []: Used to separate conditions Could be nested to group conditions They include an implicit / [TagName]: Existence <, >, =,!=: Comparators and: logical AND, to link conditions or: logical OR, to link conditions [number]: Counter 23

XPATH NAVIGATION AXES AND FUNCTIONS XPath contains 13 navigation axes. These are some we ll use: parent:: preceding-sibling:: following-sibling:: descendents: self: XPath has several functions. These are some we ll use: contains(element, text ) name() count() 24

XQUERY XQuery is a language used to make queries over an XML. XPath is actually a subset of XQuery. It s similar in concept to SQL. Queries can be nested. XQuery is out of the scope of this course. Query Query Query Data Result Data Result 25

XSLT 26

XSLT XSL: Extensible Stylesheet Language: Initial version. XSLT: XSL (with) Transformations: With some improvements. XSLT, unlike XPath, is written using XML. It s used to build templates. XSLT sees the document as a collection of nodes: elements, attributes, text, comments It s useful to find and replace parts of a XML document (using XPath). It can be used recursively. Uses structures typical of programming languages: Conditionals: (if-else) Iterators: (for-each) While using it, we should put special care with: Strange behaviors with white spaces. Implicit priorities in templates. In this section we ll learn how to use XSLT to turn a XML into a HTML. It uses the supplementary material marked as 03-XSLT, which contains a XML and several examples of how to use XSLT to turn a XML into a HTML. Running these examples in a tool like Kernow is the best way to learn. 27

XSLT ELEMENTS (I) <xsl:template> To build templates. What s inside is what s written as output. Can also be used to discard data. The match attribute targets a node or set of nodes of the XML. The value of match is an XPath expression. match= / covers all the document. match= text() covers all text, but no tags or attributes. match= * @* text() covers all text, but processes each entity independently. Be careful with template ordering. <xsl:value-of> Gets the value of a node. The select attribute specifies what s extracted. The value of select is an XPath expression. 28

XSLT ELEMENTS (II) <xsl:for-each> Iterates over a set of elements. The select attribute specifies what s iterated. The value of select is an XPath expression. <xsl:sort> Used inside a for-each. Sorts the elements. The select attribute specifies the ordering criterium. The value of select is an XPath expression. <xsl:if> Imposes a condition to select an element or not. It s used inside a for-each. The test attribute specifies the condition. 29

XML AND JAVA 30

XML & UML XML is useful for: Interoperability between different platforms. Transmitting information (streaming). Representation of tree-structured data. UML is useful for: High-level design. Data management in object-oriented languages. Visual representation of data. We want to be able to transform data from XML to UML and vice versa. So we can use the solution best-tailored to each situation. We ll learn how to make this transformation using Java since: Getting from UML to Java is trivial. Can be done in a similar way in all other object-oriented languages. 31

XML & JAVA There re several solutions that allow for the management of XML data in Java. There re two approaches: Process the XML directly: Similar to JDBC. JAXP: Java API for XML Processing. Translate between XML documents and Java objects: Similar to JPA. JAXB: Java Architecture for XML Binding. We ll learn how to use this alternative. JAXP and JAXB are the most popular XML Java libraries, but there are many others. We can also use Java to invoke the already studied XML standards, like XSLT. 32

JAXB JAXB: Java Architecture for XML Binding Included in the standard JDK since version 6. Allows us to perform two operations: Marshalling: Turn Java objects into XML documents. Unmarshalling: Turn XML documents into Java objects. We have to annotate the Java classes that will represent the data contained in the XML documents. JAXB uses the following annotations: @XmlRootElement: java.xml.bind.annotation.xmlrootelement @XmlElement: java.xml.bind.annotation.xmlelement @XmlElementWrapper: java.xml.bind.annotation.xmlelementwrapper @XmlAttribute: import java.xml.bind.annotation.xmlattribute @XmlType: java.xml.bind.annotation.xmltype @XmlTransient: java.xml.bind.annotation.xmltransient 33