Download presentation
Presentation is loading. Please wait.
Published byNaomi Strickland Modified over 9 years ago
1
E X TENSIBLE M ARKUP L ANGUAGE (XML)
2
What is XML? XML stands for EXtensible Markup Language XML is mainly designed to carry (or transmit) data, not to display data. XML is a computer language for defining markup languages to create structured documents. XML tags are not predefined. You must define your own tags
3
Difference between HTML and XML HTML is used to mark up text so it can be displayed to users on the screen. HTML describes both structure (e.g.,, ) and appearance (e.g.,, ). HTML uses a fixed, unchangeable set of tags HTML is not case-sensitive It is not strict about syntactical rules. Browsers ignore and/or correct as many HTML errors as they can. XML is used to carry data so it can be processed by computers. XML describes only content, or “meaning”. In XML, you can create your own tags XML is case-sensitive. Follows strict syntactical rules. Browsers process XML documents only if they are syntactically correct.
4
HTML and XML (similarity) 4 HTML and XML look similar, because they are both SGML languages ( Standard Generalized Markup Language) Both HTML and XML use elements enclosed in tags (Ex: This is an element ) Both use tag attributes Both use entities ( & lt;, & gt;, & amp;, & quot;, & apos; )
5
XML document structure (Basic points to remember) Each XML document should start with the version of XML The second line of your document should be the DTD, this includes the name of your DTD and its URI or location Ex: Harry Potter J K. Rowling 2005 29.99 root element of the document
6
Format of root element of the document:..... XML document structure (Basic points to remember)
7
A simple example for XML document 1960 Cessna Centurian Yellow with white trim Bangalore Karnataka
8
XML Document Contains 2 auxiliary files: One specifies tag set and rules (DTD/XML schema) Second specifies how content should be displayed (CSS/XSLT) Xml document consists of many entities: Document entity (Physically within the document) Reference entity (Separate files) - Should have name and reference (A reference to an entity has the form: & entity_name;) Binary entity (binary data)(ex: images, sound files etc)
9
CDATA & PCDATA By default, all text inside an XML document is parsed But text inside a CDATA section will be ignored by the parser. PCDATA - Parsed Character Data XML parsers normally parse all the text in an XML document. When an XML element is parsed, the text between the XML tags is also parsed: This text is also parsed The parser does this because XML elements can contain other elements, as in this example, where the element contains two other elements (first and last): Bill Gates
10
and the parser will break it up into sub-elements like this: Bill Gates CDATA - (Unparsed) Character Data The term CDATA is used about text data that should not be parsed by the XML parser. Everything inside a CDATA section is ignored by the parser. To avoid errors, script code can be defined as CDATA. A CDATA section starts with "
11
DTD - XML Building Blocks The Building Blocks of XML Documents Elements Attributes Entities PCDATA CDATA
12
What is an XML Element? An XML element is everything from the element's start tag to the element's end tag. An element can contain: other elements (element-content) Text (text content) Attributes
13
DOCUMENT TYPE DEFINITIONS
14
Document Type Definitions (DTDs) A set of structural rules called declarations, which specify a set of elements that can appear in the document as well as how and where these elements may appear. Purpose: to provide a standard form for a collection of XML documents. Not all XML documents have or need a DTD. Two types of DTDs Internal DTD (appears within a XML document) External DTD (appears as a external file – can be used with more than one document)
15
Declaring Elements within DTD DTD contains declarations that define elements, attributes, etc. Syntax: Ex: <!ELEMENT person(parent, age, spouse, sibling)> Empty Elements Empty elements are declared with the keyword - EMPTY Example:
16
In many cases, it is necessary to specify the number of times that a child element may appear. This can be done by adding a DTD modifier to the child element specification. Modifier Meaning +one or more occurrences *zero or more occurrences ? zero or one occurrence
17
Declaring attributes: In a DTD, attributes are declared with an ATTLIST declaration. An attribute declaration has the following syntax: Attribute types: there are many possible, but we will consider only CDATA DTD example: XML example:
18
The default-value can be one of the following: value - The default value of the attribute. #REQUIRED - The attribute is required. #IMPLIED - The attribute is not required. #FIXED value - The attribute value is fixed. DTD example: XML example:...
19
Declaring entities Entities are variables used to define shortcuts to standard text or special characters. Entities are normally used to specify large blocks of data that need to be repeated throughout the document. Entities can be declared in two ways: internal or external.
20
An Internal Entity Declaration Syntax: DTD Example: XML example: &writer; Note: An entity has three parts: an ampersand (&), an entity name, and a semicolon (;)
21
An External Entity Declaration Syntax: SYSTEM The keyword, specifies that the definition of the entity is in a different file. DTD Example: XML example: &writer;
22
Internal and External DTDs
23
Internal DTD Declaration If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax: <!DOCTYPE root-element [ element-declarations ]>
24
Example: !DOCTYPE note defines that the root element of this document is note. !ELEMENT note defines that the note element contains four elements: "to,from,heading,body” #PCDATA – Parsed Character Data – Indicates that browser should parse the content ]> John Robert Reminder Don't forget me
25
External DTD Declaration If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with the following syntax: Example: Jonh Robert Reminder Don't forget me The file "note.dtd“ contains the DTD:
26
XML Namespaces
27
XML Namespaces provide a method to avoid element name conflicts. To use XML Namespaces, elements are given qualified names. 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. For example, this file carries HTML table information: Apples Bananas
28
Whereas this XML file carries user defined tags: African Coffee Table 80 120 If these two files were added together, there would be a name conflict. Both contain a element, but the elements have different content and meaning. An XML parser will not know how to handle these differences. XML Namespaces (Contd.)
29
Apples Bananas African Coffee Table 80 120 Solving the Name Conflict Using a Prefix: Name conflicts in XML can easily be avoided using a name prefix. Example:
30
Apples Bananas African Coffee Table 80 120 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. xmlns:prefix=“URI”
31
Apples Bananas African Coffee Table 80 120 Namespaces can be declared in the elements where they are used or in the XML root element:
32
XML schemas
33
XML Schema is an XML-based alternative to DTD. An XML schema describes the structure of an XML document. The XML Schema language is also referred to as XML Schema Definition (XSD). What is an XML Schema? The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD. Advantage of XML Schema: One of the greatest strength of XML Schemas is the support for data types.
34
An XML Schema: defines elements that can appear in a document defines attributes that can appear in a document defines which elements are child elements defines the order of child elements defines the number of child elements defines whether an element is empty or can include text defines data types for elements and attributes defines default and fixed values for elements and attributes XML schemas (Contd.)
35
Advantages of using data types: With support for data types: It is easier to describe allowable document content It is easier to validate the correctness of data It is easier to work with data from a database It is easier to define data facets (restrictions on data) It is easier to define data patterns (data formats) It is easier to convert data between different data types
36
The element may contain some attributes. A schema declaration often looks something like this: Ex: note.xsd <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">...... Defining a schema
37
Meaning of attributes <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" The document element of XML Schemas is xs:schema. It takes the attribute xmlns:xs with the value of: http://www.w3.org/2001/XMLSchema indicating that the document should follow the rules of XML Schema. targetNamespace=http://www.w3schools.com indicates that the elements defined by this schema (note, to, from, heading, body) come from the "http://www.w3schools.com" namespace. xmlns="http://www.w3schools.com" indicates that the default namespace is: "http://www.w3schools.com". elementFormDefault="qualified" indicates that any elements used in this schema must be namespace qualified.
38
Defining a schema instance The XML Schema is like a class and XML document which adhere to an XML schema are basically instance of that schema. This XML document has a reference to an XML Schema: Tove Jani Reminder Don't forget me this weekend!
39
xmlns="http://www.w3schools.com" specifies the default namespace declaration (to be the one defined in its schema). xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" The above code indicates that this XML document is an instance of an XML Schema. xsi:schemaLocation=“http://www.w3schools.com/note.xsd” Indicates the file name where the default namespace is defined. This attribute has two values. The first value is the namespace to use. The second value is file name of the schema. Defining a schema instance (Contd.)
40
Overview of Data Types There are 2 categories of user defined XML schema data types. Simple data type: cannot have attributes & nested elements. Complex data type: have attributes & nested elements XML schema defines 44 data types.
41
Simple Types (Simple Data Types) Built-in Primitive Total 19 (string, boolean, decimal, time,date…) Derived Total 25 (token, NMTOKEN, language, name….) User Defined minLength, maxLength, pattern …
42
The following is an example of a string declaration in a schema: An element in your document might look like this: John Smith The following is an example of a date declaration in a schema: An element in your document might look like this: 2002-09-24 The decimal data type is used to specify a numeric value. The following is an example of a decimal declaration in a schema: An element in your document might look like this: 999.50
43
Complex types What is a Complex Element? A complex element is an XML element that contains other elements (child 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 Note: It is not necessary to explicitly declare that a simple-type element is a simple type, but it is necessary to specify that a complex-type element is a complex type.
44
A complex XML element, "product", which is empty: A complex XML element, "employee", which contains only child elements: John Smith A complex XML element, "food", which contains only text: Ice cream A complex XML element, "description", which contains both elements and text: It happened on 03.03.99....
45
How to Define a Complex Element John Smith Look at this complex XML element, "employee", which contains only other elements: indicates that elements must appear in the order specified. We can define a complex element in an XML Schema in two different ways: 1. The "employee" element can be declared directly by naming the element, like this:
46
2. The "employee" element can have a type attribute that refers to the name of the complex type to use: If you use the method described above, several elements can refer to the same complex type, like this:
47
Content Models: They indicate the structure and order in which child elements can appear within their parent element. sequence elements must appear in the order specified. all the elements must appear, but order is not important. choice only one of the element can appear.
48
Validating Instances of Schemas Several XML schema validation tools are available. One of them is named xsv, ( XML Schema Validator). If the schema and the instance document are available on the Web, xsv can be used online. This tool can also be downloaded and run on any computer. The Web site for xsv is: http://www.w3.org/XML/Schema#XSV
49
DISPLAYING XML DOCUMENTS WITH STYLES
50
Displaying XML documents with Styles We can use CSS and XSLT for styles. Displaying XML documents with CSS: We need two files: - the XML file (with.xml extension) and - a CSS file (with.css extension) In the XML document, we need to add one line of code to include external style sheet:
51
Displaying XML documents with XSLT style sheets XSL (eXtensible Stylesheet Language) is the preferred style sheet language of XML and is developed by W3C. XSLT is more powerful than CSS for XML. XSL consists of three parts: XSLT (XSL Transformations) - a language for transforming XML documents Xpath (XML Path Language) - a language for navigating in XML documents XSL-FO (XSL Formatting Objects) - a language for formatting XML documents
52
Difference between CSS and XSL CSS Style Sheets for HTML HTML uses predefined tags, and the meaning of each tag is well understood. For example: the tag in HTML defines a table - and a browser knows how to display it. Adding styles to HTML elements are simple. Telling a browser to display an element in a special font or color, is easy with CSS. XSL Style Sheets for XML XML does not use predefined tags (we can use any tag-names we like), and therefore the meaning of each tag is not well understood. For example: a tag could mean an HTML table, a piece of furniture, or something else (since it is user defined)- and a browser does not know how to display it. XSL describes how the XML document should be displayed and hence we should follow its syntactical rules.
53
XSLT XSL Transformations XSLT is the most important part of XSL. XSLT is used to transform an XML document into: another XML document, or another type of document that is recognized by a browser, like HTML or XHTML. Normally XSLT does this by transforming each XML element into an HTML/ XHTML element.
55
XSLT Uses XPath XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents. How Does it Work? In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document.
56
XSLT - Transformation An XML document must inform the XSLT processor that the style sheet is used by including the line: XSLT uses.xsl as file extension for its stylesheet. A style sheet must include at least one template element along with the following lines: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
57
The Element Normally a template is included to match the root node of the XML document. This can be done in two ways: - One way is to use the XPath expression “/” Ex: - another method is to use the actual root of the document. Ex:
58
XSLT Element The element is used to extract the value of an XML element and copy it to the output document. It uses a select attribute to specify the element whose contents are to be copied. Ex:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.