CREATING AN XML DOCUMENT - shms … Part1.… · Blink Malcom Gladwell 200

40
CREATING AN XML DOCUMENT Part 1

Transcript of CREATING AN XML DOCUMENT - shms … Part1.… · Blink Malcom Gladwell 200

CREATING AN XML DOCUMENT

Part 1

What is XML• XML stands for Extensible Markup Language

• XML is a subset of the Standard Generalized Markup Language (SGML)

• Describe the structure and content of data

• Organize data

• Simplifies data sharing and upgrading

• XML was designed to be clear and easily understood by non programmers

• Stores data in a plain text format (ex: Notepad)

2

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

<book><title> Blink </title><author> Malcom Gladwell </author><NumOfPages> 200 </NumOfPages>

</book><book>

<title> Outliers </title><author> Malcom Gladwell </author><NumOfPages> 300 </NumOfPages>

</book></bookStore>

3

XML DeclarationThe first line is the XML declaration. It tells the processor that what follows is written using XML.

<?xml version="1.0" encoding="UTF-8“ ß default value?>

4

ElementsElements contain an opening tag and a closing tag. It is containing data

to be stored in the document.

Example: <opening_tag> content </closing_tag>

• Element names are usually selected by XML authors.Rules for Naming Elements:• case sensitive Book =/= book• Must begin with a letter or an underscore (_), ex: “<First_Name>”• cannot contain blank spaces• cannot begin with “xml”• the name in the closing tag must match the name

in the opening tag

5

The Element Hierarchy• The document body has a hierarchical structure (tree

structure)

• Start with the root element of the document

• A child element is nested inside its parent element

• Elements that are side-by-side in a hierarchy are sibling elements

• All elements are children of the root element

6

The Element HierarchyXML Structure : Tree Structure :<root>

<child><subchild1>.....</subchild1><subchild2>.....</subchild2>

</child>< /root>

subchild1 and subchild2 are siblings

7

Root

Child

subchild subchild

Comments<!-- your comment here -->

Example:

<?xml version="1.0“ encoding="UTF-8” ?><!-- this is a book store --><bookStore>

<book><title> Blink </title><author> Malcom Gladwell </author><NumOfPages> 200 </NumOfPages>

</book></bookStore>

8

Attributes• An attribute describes a feature or characteristic of an element. The

element can contain one or more attributes.

The syntax: <element attribute=“value”> content </element>

Rules for Naming Attributes:• case sensitive Book =/= book

• begin with a letter or an underscore (_)

• cannot contain blank spaces

• cannot begin with “xml”

• an attribute name can appear only once within an

element9

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

<book id=“111” language=“english”><title> Blink </title><author> Malcom Gladwell </author><NumOfPages> 200 </NumOfPages>

</book><book id=“222” language=“arabic”>

<title> Outliers </title><author> Malcom Gladwell </author><NumOfPages> 300 </NumOfPages>

</book></bookStore>

10

Attributes

EMPTY ELEMENTS AND ATTRIBUTES

• An empty element is an element that contains no content. <customers>

<customer ID=”111”><name> Ahmed </name><address> 14 Bronson st. </address><orders>

<order number=”24”/><order number=”62”/>

</orders></customer>

</customers>• Empty attribute:

<element attribute=” " />

HTML vs. XML1) describes the presentation of a document

2) predefined elements<head></head><body></body>

1) describes the contentof a document

2) you create your own elements<reminder>…</reminder>

12

XML VOCABULARIES• A set of XML elements for a particular industry or

business function.

13

14

XML Vocabulary Examples

15

ChemicalMarkupLanguage

ENTITY AND CHARACTER REFERENCES

Special characters, such as the symbol for the British pound, can be inserted into your XML document by using entity references or character reference.

ex: <course>&quot; HTML &quot;</course>

ENTITY AND CHARACTER REFERENCES

ENTITY AND CHARACTER REFERENCES

<teacher><name> Sara Alotaiby </name><num>12345</num><salary>7000</salary><list_of_courses>

<course>&quot; HTML &quot;</course><course>C++</course><course>networking</course>

</list_of_courses></teacher>

• A common mistake in XML documents is to forget that the ampersand symbol (&).o For example

• <artist>Miles Davis & John Coltrane</artist> Wrong.• <artist>Miles Davis &amp; John Coltrane</artist> Right.

CDATA Sections

CDATA SECTIONS

• A CDATA section is a large block of text the XML processor will interpret only as text.

• The syntax to create the CDATA section is:

<! [CDATA [Text Block

] ]>

CDATA SECTIONS

In this example, a CDATA section includes “ , > and < without references:

<! [CDATA [“Salaries” must be >5000$ and <10000$

] ]>

CDATA SECTIONS

A CDATA section:● May be placed anywhere within a document● Cannot be nested within other CDATA sections● Cannot be empty● Cannot contain the sequence of symbols ]]> because this

sequence marks the end of a CDATA section.

Example of CDATA SECTIONS

<teachers><![CDATA[this is an "XML" course , students' passsing grade is >60]]><teacher>

<name> Sara Alotaiby </name><num>12345</num><salary>7000</salary>

</teacher></teachers>

PARSING AN XML DOCUMENT

DISPLAYING AN XML DOCUMENT IN A WEB BROWSER

If there are no syntax errors. Internet Explorer will display the document’s contents in an expandable/collapsible outline format including all markup tags.

Error in XML Document

• Error message produced by an XML document that is not well formed.

RSS • RSS stands for: Real Simple Syndication• distribute up-to-date web content from one website

to thousands of other websites around the world.• RSS is written in XML

28

29

CSS Style Sheets

LINKING TO A STYLE SHEET

Link the XML document to a stylesheet to format the document. The XML processor will combine the style sheet with the XML document and apply any formatting codes defined in the stylesheet to display a formatted document.

There are two main style sheet languages used with XML:– Cascading Style Sheets (CSS) – Extensible StyleSheets (XSL)

LINKING TO A STYLE SHEET

There are some important benefits to using stylesheets:

– By separating content from format, you can concentrate on the appearance of the document– Different style sheets can be applied to the same XML document– Any style sheet changes will be automatically reflected in any Web page based upon the stylesheet

APPLYING A STYLE TO AN ELEMENT

• To apply a stylesheet to a document, use the following syntax:

selector {attribute1:value1; attribute2:value2; …}

• selector is an element (or set of elements) from the XMLdocument.attribute and value are the style attributes and attribute values to be applied to the document.

APPLYING A STYLE TO AN ELEMENT

For example:

artist { color:red; font-weight:bold; }

will display the text of the artist element in a red boldface type.

CREATING PROCESSING INSTRUCTIONS

• The link from the XML document to a style sheet is created using a processing statement.

• A Processing Instruction is a command that gives instructions to the XML parser.

CREATING PROCESSING INSTRUCTIONS

• For example:

<?xml-stylesheet type=“style” href=“sheet” ?>

Style is the type of style sheet to access and sheet is the name and location of the style sheet.

teachers.xml

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/css" href="teachers.css" ?><teachers><![CDATA[ this is an "XML" course, students' passing grade is >60 ]]><teacher>

<name> Sara Alotaiby </name><num>12345</num><salary>7000</salary><list_of_courses>

<course>&quot; HTML &quot;</course><course>C++</course><course>networking</course>

</list_of_courses></teacher>………. more teacher entries …...</teachers>

teachers.css

teachers { color:green; }teacher { display: block;

width:400px;color:blue;text-align:center;margin:10px;padding:15px; }

salary { color:red; }

teachers.xml formatted with teachers.css

Thank you

40