Download presentation
Presentation is loading. Please wait.
Published byFrederick Williams Modified over 8 years ago
1
XML
2
What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive XML is a W3C Recommendation
3
The Difference Between XML and HTML XML is not a replacement for HTML. XML and HTML were designed with different goals: – XML was designed to transport and store data, with focus on what data is – HTML was designed to display data, with focus on how data looks HTML is about displaying information, while XML is about carrying information.
4
With XML You Invent Your Own Tags Tove Jani Reminder Don't forget me this weekend!
5
XML XML Separates Data from HTML XML Simplifies Data Sharing XML Simplifies Data Transport XML Simplifies Platform Changes XML Makes Your Data More Available
6
XML is Used to Create New Internet Languages Here are some examples: XHTML WSDL for describing available web services WAP and WML as markup languages for handheld devices RSS languages for news feeds RDF and OWL for describing resources and ontology SMIL for describing multimedia for the web Chemical Markup Language (ChemML) Mathematical Markup Language (MathML) …
7
Example Tove Jani Reminder Don't forget me this weekend! The first line is the XML declaration. It defines the X The next line describes the root element of the document (like saying: "this document is a note"):ML version (1.0). The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
8
XML Documents Form a Tree Structure
9
Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95
10
Details All XML Elements Must Have a Closing Tag XML Tags are Case Sensitive XML Elements Must be Properly Nested XML Documents Must Have a Root Element Comments in XML:
11
Attributes XML elements can have attributes in name/value pairs just like in HTML. In XML, the attribute values must always be quoted.
12
What is an XML Element? An XML element is everything from (including) the element's start tag to (including) the element's end tag. An element can contain: – other elements – text – attributes – or a mix of all of the above...
13
XML Elements are Extensible Tove Jani Don't forget me this weekend! 2008-01-10 Tove Jani Reminder Don't forget me this weekend! Should the application break or crash? No. The application should still be able to find the,, and elements in the XML document and produce the same output. One of the beauties of XML, is that it can be extended without breaking applications.
14
XML Elements vs. Attributes Anna Smith female Anna Smith Same Information. However, prefer below
15
Avoid XML Attributes? Some of the problems with using attributes are: – attributes cannot contain multiple values (elements can) – attributes cannot contain tree structures (elements can) – attributes are not easily expandable (for future changes)
16
Attributes Are Not That Useless Tove Jani Reminder Don't forget me this weekend! Jani Tove Re: Reminder I will not
17
Name Conflicts In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications. If these XML fragments were added together, there would be a name conflict. Apples Bananas African Coffee Table 80 120
18
Solving the Name Conflict Using a Prefix Name conflicts in XML can easily be avoided using a name prefix. Apples Bananas African Coffee Table 80 120
19
XML Namespaces - The xmlns Attribute When using prefixes in XML, a so- called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".
20
Apples Bananas African Coffee Table 80 120 http://www.w3.org/TR/html4/ Apples Bananas African Coffee Table 80 120 http://www.w3.org/TR/html4/ Namespaces can be declared in the elements where they are used or in the XML root element:
21
Well Formed XML Documents An XML document with correct syntax is "Well Formed". – XML documents must have a root element – XML elements must have a closing tag – XML tags are case sensitive – XML elements must be properly nested – XML attribute values must be quoted
22
Valid XML Documents A valid XML document is not the same as a well formed XML document. The first rule, for a valid XML document, is that it must be well formed. The second rule is that a valid XML document must conform to a document type. Rules that defines legal elements and attributes for XML documents are often called document definitions, or document schemas.
23
When to Use a Document Definition? A document definition is the easiest way to provide a reference to the legal elements and attributes of a document. A document definition also provides a common reference that many users (developers) can share. A document definition provides a standardization that makes life easier.
24
When NOT to Use a Document Definition? XML does not require a document definition. When you are experimenting with XML, or when you are working with small XML files, creating document definitions may be a waste of time. If you develop applications, wait until the specification is stable, before you add a document definition. Otherwise your software might stop working, because of validation errors.
25
Document Definitions
26
XML DTD A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a DTD: Tove Jani Reminder Don't forget me this weekend! The DOCTYPE declaration, in the example above, is a reference to an external DTD file. The content of the file is shown in the next slide.
27
The DTD above is interpreted like this: !DOCTYPE note defines that the root element of the document is note !ELEMENT note defines that the note element contains four elements: "to, from, heading, body" !ELEMENT to defines the to element to be of type "#PCDATA" !ELEMENT from defines the from element to be of type "#PCDATA" !ELEMENT heading defines the heading element to be of type "#PCDATA" !ELEMENT body defines the body element to be of type "#PCDATA" ]>
28
XML Schema XML Schema is an XML-based alternative to DTD: The Schema above is interpreted like this: defines the element called "note" the "note" element is a complex type the complex type is a sequence of elements the element "to" is of type string (text) the element "from" is of type string the element "heading" is of type string the element "body" is of type string
29
XML Schemas are More Powerful than DTD XML Schemas are written in XML XML Schemas are extensible to additions XML Schemas support data types XML Schemas support namespaces
30
Why Use an XML Schema? With XML Schema, your XML files can carry a description of its own format. With XML Schema, independent groups of people can agree on a standard for interchanging data. With XML Schema, you can verify data.
31
XML Schemas Support Data Types One of the greatest strength of XML Schemas is the support for data types: – It is easier to describe document content – It is easier to define restrictions on data – It is easier to validate the correctness of data – It is easier to convert data between different data types
32
XML Schema The element is the root element of every XML Schema:...... This XML document has a reference to an XML Schema: Tove Jani Reminder Don't forget me this weekend!
33
Defining a Simple Element The syntax for defining a simple element is: – XML Schema has a lot of built-in data types. The most common types are: – xs:string – xs:decimal – xs:integer – xs:boolean – xs:date – xs:time
34
Example XML and XSD XML Refsnes 36 1970-03-27 XML Refsnes 36 1970-03-27 XSD XSD
35
Define an Attributes The syntax for defining an attribute is: – XML Schema has a lot of built-in data types. The most common types are (again): – xs:string – xs:decimal – xs:integer – xs:boolean – xs:date – xs:time
36
Example XML Smith XML Smith XSD XSD Also take a look at this
37
XSD Restrictions/Facets The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120:
38
XSD Restrictions/Facets
39
XSD Complex Elements A complex element is an XML element that contains other elements and/or attributes. There are four kinds of complex elements: – empty elements – elements that contain only other elements – elements that contain only text – elements that contain both other elements and text Each of these elements may contain attributes as well!
40
Example XML John Smith XML John Smith XSD XSD
41
Another Example The "employee" element can have a type attribute that refers to the name of the complex type to use: XSD XSD
42
Xpath XPath is a language for finding information in an XML document. XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.
43
XPath Terminology In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.
44
Relationship of Nodes Parent Children Siblings - Nodes that have the same parent. Ancestors – Node’s parents and their parents… Descendants – Nodes childeren and their childeren….
45
XPath Syntax Base XML Document: Harry Potter 29.99 Learning XML 39.95
46
Selecting Nodes ExpressionDescription nodenameSelects all nodes with the name "nodename" /Selects from the root node //Selects nodes in the document from the current node that match the selection no matter where they are.Selects the current node..Selects the parent of the current node @Selects attributes Path ExpressionResult bookstoreSelects all nodes with the name "bookstore" /bookstoreSelects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! bookstore/bookSelects all book elements that are children of bookstore //bookSelects all book elements no matter where they are in the document bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element //@langSelects all attributes that are named lang
47
Predicates Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets. Next Slide gives you some examples
48
Path ExpressionResult /bookstore/book[1]Selects the first book element that is the child of the bookstore element. /bookstore/book[last()]Selects the last book element that is the child of the bookstore element /bookstore/book[last()-1]Selects the last but one book element that is the child of the bookstore element /bookstore/book[position()<3]Selects the first two book elements that are children of the bookstore element //title[@lang]Selects all the title elements that have an attribute named lang //title[@lang='eng']Selects all the title elements that have an attribute named lang with a value of 'eng' /bookstore/book[price>35.00]Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 /bookstore/book[price>35.00]/titleSelects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00
49
Selecting Unknown Nodes XPath wildcards can be used to select unknown XML elements. WildcardDescription *Matches any element node @*Matches any attribute node node()Matches any node of any kind Path ExpressionResult /bookstore/*Selects all the child nodes of the bookstore element //*Selects all elements in the document //title[@*]Selects all title elements which have any attribute
50
XQuery XQuery is to XML what SQL is to database tables. XQuery is a language for finding and extracting elements and attributes from XML documents.
51
Base XML Example Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 Learning XML Erik T. Ray 2003 39.95
52
Functions XQuery uses functions to extract data from XML documents. The doc() function is used to open the "books.xml" file: – doc("books.xml")
53
Path Expressions XQuery uses path expressions to navigate through elements in an XML document. The following path expression is used to select all the title elements in the "books.xml" file: – doc("books.xml")/bookstore/book/title Everyday Italian Harry Potter XQuery Kick Start Learning XML
54
Predicates XQuery uses predicates to limit the extracted data from XML documents. The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30: – doc("books.xml")/bookstore/book[price<30] Harry Potter J K. Rowling 2005 29.99
55
XQuery FLWOR Expressions For, Where, Order by and Return doc("books.xml")/bookstore/book[price>30]/title for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title XQuery Kick Start Learning XML Learning XML XQuery Kick Start
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.