Transcript Tutorial 12: Programming with AJAX
SPECIAL TOPIC
XML
Introducing XML
XML (eXtensible Markup Language)
◦ A language used to create structured documents XML vs HTML ◦ XML is designed to transport and store data (carry) ◦ ◦ ◦ ◦ HTML was designed to display data (show) Shares common structure with HTML documents Elements are indicated with markup tags that contain textual content; element names are descriptive A markup tag can contain attributes that describe a feature of the element, and a single root element contains all other elements in the document
Introducing XML
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
XML Tree Structure
With XML You Invent Your Own Tags
◦ the XML language has no predefined tags XML documents form a tree structure that starts at "the root" and branches to "the leaves".
.
.
Introducing XML
XML declaration
It defines the XML version (1.0) and the encoding used (UTF-8).
Single root element (students) Student, name, and photo elements and the id and grade attributes provide information about individual students
o o o o o o o o
Syntax Rules
Must have closing tags Case sensitive tags Proper tag nesting Must have a Root element Attributes must be quoted Uses HTML comments Special symbols > < & ‘ “ Use entity references: < >
Syntax Rules
Comments are similar to HTML comment White-space is Preserved in XML Naming rules ◦ XML elements must follow these naming rules: ◦ Names can contain letters, numbers, and other characters ◦ Names cannot start with a number or punctuation character ◦ Names cannot start with the letters xml (or XML, or Xml, etc) ◦ Names cannot contain spaces ◦ Any name can be used, no words are reserved
XML Elements
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
XML attributes
XML elements can have attributes.
◦ Attributes provide additional information about an element.
XML Elements vs. Attributes
Take a look at these examples: < person sex="female">
Attributes vs. Elements
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) Attributes are difficult to read and maintain ◦ Use elements for data, and attributes for information that is not relevant to the data
Namespaces
XML Namespaces provide a method to avoid element name conflicts Defined by the xmlns attribute Tags that are not HTML tags have the prefix xsl, identified by the namespace xmlns:xsl="http://www.w3.org/1999/XSL/Transform": Name conflicts in XML can easily be avoided using a name prefix xmlns:h="http://www.w3schools.com/html4"> xmlns:f="http://www.w3schools.com/furniture">
e
X
tensible
S
tyle sheet
L
anguage
T
ranslation
.XSLT
Styles
Can use a CSS style sheet to format an XML document Better approach is to use XSL (eXtensible Stylesheet Language) ◦ can transform an XML document into HTML, using ◦ XSLT (eXtensible Stylesheet Language Transformation)
Transforming XML with XSLT
Extensible Stylesheet Language Transformation (XSLT)
◦ A style language developed for XML documents ◦ Allows developers to easily transform contents of an XML document into another document format ◦ A more efficient approach to transforming an XML document into HTML format than working with the document object model
The Element
The match attribute is used to associate a template with an XML element < xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
My CD Collection
Title | Artist |
---|---|
. | . |
Used to extract the value of an XML element and add to output.
< xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> < xsl:template match="/">
My CD Collection
select=" catalog/cd/artist "/>Title |
---|
Artist |
|
< /xsl:template>
< /xsl:stylesheet>
The Element
Allows you to do looping < xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> < xsl:template match="/">
My CD Collection
Title | Artist |
---|---|
| |
XSLT Element used to sort the output
< xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> < xsl:template match="/">
My CD Collection
Title | Artist |
---|---|
| |
XSLT Element
used to put a conditional test against the content of the XML file < xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> < xsl:template match="/">
My CD Collection
Title | Artist |
---|---|
< xsl:value-of select="title "/> | < xsl:value-of select="artist "/> |