Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Monday, 1 August 2011

Part 6 - CSS & XSLT | XML

With CSS (Cascading Style Sheets) you can add display information to an XML document.

  Displaying your XML Files with CSS?

It is possible to use CSS to format an XML document.
Below is an example of how to use a CSS style sheet to format an XML document:
Take a look at this XML file: The CD catalog
Then look at this style sheet: The CSS file
Finally, view: The CD catalog formatted with the CSS file
Below is a fraction of the XML file. The second line links the XML file to the CSS file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="/cd_catalog.css"?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
</CATALOG>
Formatting XML with CSS is not the most common method.

  Displaying XML with XSLT

XSLT is the recommended style sheet language of XML.
XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than CSS.
One way to use XSLT is to transform XML into HTML before it is displayed by the browser as demonstrated in these examples:
View the XML file, the XSLT style sheet, and View the result.
Below is a fraction of the XML file. The second line links the XML file to the XSLT file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="/simple.xsl"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>two of our famous Belgian Waffles</description>
    <calories>650</calories>
  </food>
</breakfast_menu>
If you want to learn more about XSLT, find our XSLT tutorial on w3schools homepage.

  Transforming XML with XSLT on the Server

In the example above, the XSLT transformation is done by the browser, when the browser reads the XML file.
Note that the result of the output is exactly the same, either the transformation is done by the web server or by the web browser.

Part 5 - Validation & Viewing | XML

XML with correct syntax is "Well Formed" XML.
XML validated against a DTD is "Valid" XML.

  Well Formed XML Documents

A "Well Formed" XML document has correct XML syntax.
The syntax rules were described in the previous chapters:
  • 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
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


  Valid XML Documents

A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The DOCTYPE declaration in the example above, is a reference to an external DTD file. The content of the file is shown in the paragraph below.

  XML DTD

The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements:
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
If you want to study DTD, you will find our DTD tutorial on w3schools homepage.

  XML Schema

<xs:element name="note">

<xs:complexType>
  <xs:sequence>
    <xs:element name="to" type="xs:string"/>
    <xs:element name="from" type="xs:string"/>
    <xs:element name="heading" type="xs:string"/>
    <xs:element name="body" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

</xs:element>

  A General XML Validator

To help you check the syntax of your XML files, we have created an XML validator to syntax-check your XML.

  XML Viewers

If you are attempting to use XML for the first time, chances are you don't know or have the tools that are necessary to view XML files. The good news is there are a plethora of free viewers readily available to you. This lesson will show the many free XML viewers that are currently out there.

  Free XML Viewer

Chances are the web browser you have on your computer is XML ready. If you would like to see how your browser handles XML files, open up our XML File: class_list.xml. If your browser doesn't let you view that XML file, then read on as we describe the popular browsers and their XML viewing features.

  Internet Explorer XML Viewer

Internet Explorer 6.0 supports viewing XML files. You can download IE 6.0 from Microsoft's Website. Internet Explorer 6.0 has special color coding the make XML documents easier to read. Also, next to expandable elements (those that contains child elements) there is a plus or minus sign (+/-) that lets you expand or contract that element.
After you have installed Internet Explorer 6.0, try viewing our XML file: class_list.xml.

  Firefox XML Viewer

Firefox has some of the same features as Internet Explorer. You can download Mozilla's Firefox from Mozilla.com.
After you have installed Internet Explorer 6.0, try viewing our XML file: class_list.xml

  Opera XML Viewer

Unfortunately, Opera does not currently support viewing XML files, so please download one of the browsers we have listed above. If you would like to see what XML looks like in Opera, open our XML file: class_list.xml.
If you are looking for an XML editor, check out our next lesson, in which we recommend an XML editor based on your needs and budget!

Part 4 - Elements&Attributes | XML

  XML Element

XML is a markup language that is used to store data in a self-explanatory manner. Making the data "self-explanatory" comes about by containing information in elements. If a piece of text is a title then it will be contained within a "title" element.

  XML Tag

A tag is just a generic name for a <element>. An opening tag looks like <element>, while a closing tag has a slash that is placed before the element's name: </element>. From now on we will refer to the opening or closing of an element as open or close tags. All information that belongs to an element must be contained between the opening and closing tags of an element.

  XML Attribute

Attributes are used to specify additional information about the element. It may help to think of attributes as a means of specializing generic elements to fit your needs. An attribute for an element appears within the opening tag.
If there are multiple values an attribute may have, then the value of the attribute must be specified. For example, if a tag had a color attribute then the value would be: red, blue, green, etc. The syntax for including an attribute in an element is:
  • <element attributeName="value">
In this example we will be using a madeup XML element named "friend" that has an optional attribute age.

  XML Code:

<friend age="23">Samantha</friend>

  Element Review

  • Elements are used to classify data in an XML document so that the data becomes "self-explanatory".
  • Opening and closing tags represent the start and end of an element.
  • Attributes are used to include additional information on top of the data that falls between the opening and closing tag.

  XPath - Attribute

You have already learned how to select any element in an XML document, but how would you get that element's attribute? Those attributes values are within your reach once you learn how to use @ !

  XML Code (Example)

<inventory>
 <drink>
<lemonade supplier="mother" id="1">
<price>$2.50</price>
<amount>20</amount>
</lemonade>
<pop supplier="store" id="2">
<price>$1.50</price>
<amount>10</amount>
</pop>
</drink>
<snack>
<chips supplier="store" id="3">
<price>$4.50</price>
<amount>60</amount>
<calories>180</calories>
</chips>
</snack>
</inventory>

  XPath - @ is for Attribute!

After you have figured out how to select an element in your XML document, just take it one step further to get the attribute.
At the end of your XPath expression, which is normally the element you want to select, add the at sign "@" plus the name of the attribute you wish to select. The following XPath expression selects chips element.

  XPath Expression:

inventory/snack/chips
If we wanted to select the supplier attribute of chips, we would add "@supplier" to our expression.

  XPath Expression:

inventory/snack/chips@supplier
If we wanted to select the supplier attribute of pop, the XPath expression would have to be changed slightly.

  XPath Expression:

inventory/drink/pop@supplier

Part 3 - Syntax | XML

The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to use.

XML  All XML Elements Must Have a Closing Tag

In HTML, you will often see elements that don't have a closing tag:
<p>This is a paragraph
<p>This is another paragraph
In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
<p>This is a paragraph</p>
<p>This is another paragraph</p>
Note: You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself, and it has no closing tag.

XML  XML Tags are Case Sensitive

XML elements are defined using XML tags.
XML tags are case sensitive. With XML, the tag <Letter> is different from the tag <letter>.
Opening and closing tags must be written with the same case:
<Message>This is incorrect</message>
<message>This is correct</message>
Note: "Opening and closing tags" are often referred to as "Start and end tags". Use whatever you prefer. It is exactly the same thing.

XML  XML Elements Must be Properly Nested

In HTML, you might see improperly nested elements:
<b><i>This text is bold and italic</b></i>
In XML, all elements must be properly nested within each other:
<b><i>This text is bold and italic</i></b>
In the example above, "Properly nested" simply means that since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.

XML  XML Documents Must Have a Root Element

XML documents must contain one element that is the parent of all other elements. This element is called the root element.
<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>


XML  XML Attribute Values Must be Quoted

XML elements can have attributes in name/value pairs just like in HTML.
In XML the attribute value must always be quoted. Study the two XML documents below. The first one is incorrect, the second is correct:
<note date=12/11/2007>
  <to>Tove</to>
  <from>Jani</from>
</note>

<note date="12/11/2007">
  <to>Tove</to>
  <from>Jani</from>
</note>
The error in the first document is that the date attribute in the note element is not quoted.

XML  Entity References

Some characters have a special meaning in XML.
If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.
This will generate an XML error:
<message>if salary < 1000 then</message>
To avoid this error, replace the "<" character with an entity reference:
<message>if salary < 1000 then</message>
There are 5 predefined entity references in XML:
< < less than
> > greater than
& & ampersand 
' ' apostrophe
" " quotation mark
Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.

XML  Comments in XML

The syntax for writing comments in XML is similar to that of HTML.
<!-- This is a comment -->

XML  White-space is Preserved in XML

HTML truncates multiple white-space characters to one single white-space:
HTML: Hello           my name is Tove
Output: Hello my name is Tove.
With XML, the white-space in a document is not truncated.

XML  XML Stores New Line as LF

In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF). The character pair bears some resemblance to the typewriter actions of setting a new line. In Unix applications, a new line is normally stored as a LF character. Macintosh applications use only a CR character to store a new line.

Part 2 - Tree (Organisation) | XML

XML documents form a tree structure that starts at "the root" and branches to "the leaves".

  An Example XML Document

XML documents use a self-describing and simple syntax:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set).
The next line describes the root element of the document (like saying: "this document is a note"):
<note>
The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

And finally the last line defines the end of the root element:
</note>
You can assume, from this example, that the XML document contains a note to Tove from Jani.
Don't you agree that XML is pretty self-descriptive?

  XML Documents Form a Tree Structure

XML documents must contain a root element. This element is "the parent" of all other elements.
The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree.
All elements can have sub elements (child elements):
<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>
The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have children. Children on the same level are called siblings (brothers or sisters).
All elements can have text content and attributes (just like in HTML).

Example:

DOM node tree The image above represents one book in the XML below:
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
The root element in the example is <bookstore>. All <book> elements in the document are contained within <bookstore>.
The <book> element has 4 children: <title>,< author>, <year>, <price>.

Part 1 - Introduction | XML

XML was designed to transport and store data.
HTML was designed to display data.

XML  What You Should Already Know

Before you continue you should have a basic understanding of the following:
  • HTML
  • JavaScript

XML  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

XML  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.

XML  XML Does not DO Anything

Maybe it is a little hard to understand, but XML does not DO anything. XML was created to structure, store, and transport information.
The following example is a note to Tove from Jani, stored as XML:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The note above is quite self descriptive. It has sender and receiver information, it also has a heading and a message body.
But still, this XML document does not DO anything. It is just pure information wrapped in tags. Someone must write a piece of software to send, receive or display it.

XML  XML is Just Plain Text

XML is nothing special. It is just plain text. Software that can handle plain text can also handle XML.
However, XML-aware applications can handle the XML tags specially. The functional meaning of the tags depends on the nature of the application.

XML  With XML You Invent Your Own Tags

The tags in the example above (like <to> and <from>) are not defined in any XML standard. These tags are "invented" by the author of the XML document.
That is because the XML language has no predefined tags.
The tags used in HTML (and the structure of HTML) are predefined. HTML documents can only use tags defined in the HTML standard (like <p>, <h1>, etc.).
XML allows the author to define his own tags and his own document structure.

XML  XML is Not a Replacement for HTML

XML is a complement to HTML.
It is important to understand that XML is not a replacement for HTML. In most web applications, XML is used to transport data, while HTML is used to format and display the data.
My best description of XML is this:
XML is a software- and hardware-independent tool for carrying information.
 

E-Learning Copyright © 2011 Powered by Blogger