eXtensible Markup Language

advertisement
eXtensible Markup Language
XML is a kind of a ‘Mark-up Language.’
Any markup language have:
• Tags and Elements:
ex. <p> is a tag.
and <p> with the text here </p> <--- is an element.
• Attributes:
ex. <tagname attribute = “value”>
All Mark up languages are created from:
“Standardized Generalized Markup Language.” (SGML)
• SGML became an international standard in 1986.
• SGML allows the authors to create and describe additional elements and
attributes in a mark up language.
• HTML came from SGML, as did XML
HTML : Some Drawbacks
•
•
•
•
Has fixed set of tags. You cannot create your own.
Is only a presentation technology. Does not carry information about the meaning
of the content held within its tags.
Flat. No precedence among tags.
High traffic volume. Ex. Sending large general sets of data across a network when
only a small amount is required.
XML:
• XML was designed to describe data
• Tags are not predefined in XML. You must define your own tags
• XML uses a Document Type Definition (DTD) or an XML Schema to describe
the data
• XML with a DTD or XML Schema is designed to be self-descriptive
The main difference between XML and HTML
• XML was designed to carry data.
• XML is not a replacement for HTML.
XML and HTML were designed with different goals:
• XML was designed to describe data and to focus on what data is.
HTML was designed to display data and to focus on how data looks.
• HTML is about displaying information, while XML is about describing
information
Syntax:
XML tags are case sensitive.
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
 Root element
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
 End of Root element.
General Form:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
Example with attributes:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
</note>
XML validation:
•
•
•
XML with correct syntax is Well Formed XML.
XML validated against a DTD is Valid XML.
A "Well Formed" XML document is a document that conforms to the XML
syntax
XML DTD (Document Type Definition)
•
•
A DTD defines the legal elements of an XML document.
The purpose of a DTD is to define the legal building blocks of an XML
document. It defines the document structure with a list of legal elements.
XML Schema:
XML Schema is an XML based alternative to DTD.
•
•
The schema is how we assign the data types to each tag and any attributes that are
contained in the XML document.
A schema is a structured document which must obey XML syntax rules. It is
composed of a series of predefined tags and attributes that are part of the XML
language and are used to set the data types for the values associated with our
custom tags.
A schema can be part of the XML document or a separate file.
•
•
Syntax:
Schema
The Schema tag serves as a container element that delimits the beginning and end of the
schema. This tag must be closed and please note the exact spelling with regard to case.
xmlns
The xmlns attribute is used to declare the schema XML namespace. The value is a URL
or URN address that the browser will access to get information on how to parse and
validate the code.
xmlns:dt
The xmlns:dt attribute is used to declare the data types of the schema XML namespace.
The value is a URL or URN address that the browser will access to get information on
the data types to allow code validation.
If you are using IE 5 to view your XML document, then you must include the xmlns and
the xmlns:dt attributes exactly as displayed below:
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemasmicrosoft-com:datatypes">
...
</Schema>
An example with Schema:
Code for aquarium.xml:
<?xml version="1.0"?>
<aquarium xmlns="x-schema:aquariumSchema.xml">
<fish>neon tetra</fish>
</aquarium>
Code for aquariumSchema.xml:
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemasmicrosoft-com:datatypes">
<!-- since the fish element contains no elements or attributes -->
<!-- we close using the space blackslash -->
<ElementType name="fish" content="textOnly" dt:type="string" />
<!-- the aquarium element contains the fish element -->
<!-- so we use a closing /ElementType tag -->
<ElementType name="aquarium" content="mixed">
<element type="fish" />
</ElementType>
</Schema>
===============================================================
Displaying an XML Document:
The data in an XML document cannot be directly displayed on a browser.
Therefore we must apply additional technology to create an HTML page that contains the
XML data. We will discuss two ways to do this:
1. Data Binding
2. Style sheets
Data Binding:
Internet Explorer 4 and 5 contain a DSO (Data Source Object) that permits displaying the
data in an XML document on an HTML page by using the technology of client-side data
binding.
This involves:
• Taking the XML data from the server-side
• Transferring the data to the client-side
• Caching the data on the client-side for future access,
• And binding the data to an HTML tag. The data is treated like a record set in
ADO and the data can be manipulated by using the various methods and
properties of the ADO Recordset object.
Example:
<?xml version="1.0" ?>
- <devguru_staff>
- <programmer>
<name>Bugs Bunny</name>
<dob>03/21/1970</dob>
<ssn>111-11-1111</ssn>
</programmer>
- <programmer>
<name>Daisy Duck</name>
<dob>08/09/1949</dob>
<ssn>222-22-2222</ssn>
</programmer>
- <programmer>
<name>Minnie Mouse</name>
<dob>04/13/1977</dob>
<ssn>333-33-3333</ssn>
</programmer>
- <programmer>
<name>Pluto</name>
<dob>07/04/1979</dob>
<ssn>444-44-4444</ssn>
</programmer>
- <programmer>
<name>Porky Pig</name>
<dob>11/30/1956</dob>
<ssn>555-55-5555</ssn>
</programmer>
- <programmer>
<name>Road Runner</name>
<dob>01/19/1953</dob>
<ssn>666-66-6666</ssn>
</programmer>
</devguru_staff>
Our goal is to create an HTML page that displays the XML data for the date of
birth (dob), name (name), and social security number (ssn) for one staff member at a
time. Further, we want to be able to scroll through the list of staff members using
navigation buttons and see the data for any staff members.
To accomplish this, we will:
• use the src attribute of an inline xml element to link our HTML page to the XML
document.
• use the HTML span tag for the data binding.
• use the datasrc attribute to link the each span element to the xml element.
• use the datafld attribute to bind the specified XML data value to the span
element.
• use previous and next input buttons to call data navigation functions.
• use the MoveNext and MovePrevious ADO methods to navigate our data.
• use the RecordCount and AbsolutePosition ADO properties to limit our data
navigation so that we never go to BOF or EOF.
Code:
<html>
<title>Data Binding Example</title>
<body>
<xml id="xmlStaff" src="devguru_staff_list.xml">
</xml>
<b>Use the buttons to scroll up and down the DevGuru Staff List</b>
<br><br>
NAME: <span datasrc="#xmlStaff" datafld="name"></span>
<br>
DOB: <span datasrc="#xmlStaff" datafld="dob"></span>
<br>
SSN: <span datasrc="#xmlStaff" datafld="ssn"></span>
<br> <br>
<input type="button" value="Previous" onclick="Previous()">
<input type="button" value="Next" onclick="Next()">
<script type="text/javascript">
function Previous()
{
if(xmlStaff.recordset.AbsolutePosition > 1)
xmlStaff.recordset.movePrevious();
}
function Next()
{
if(xmlStaff.recordset.AbsolutePosition < xmlStaff.recordset.RecordCount)
xmlStaff.recordset.moveNext();
}
</script>
</body>
</html>
Display:
Use the buttons to scroll up and down the Staff List
NAME: Bugs Bunny
DOB: 03/21/1970
SSN: 111-11-1111
Back
Next
Display using style sheets:
Source code for cdCatalog.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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>
Source code for style sheet:
CATALOG
{
background-color: #ffffff;
width: 100%;
}
CD
{
display: block;
margin-bottom: 30pt;
margin-left: 0;
}
TITLE
{
color: #FF0000;
font-size: 20pt;
}
ARTIST
{
color: #0000FF;
font-size: 20pt;
}
COUNTRY,PRICE,YEAR,COMPANY
{
Display: block;
color: #000000;
margin-left: 20pt;
}
DISPLAY:
The CD Catalog formatted with the CSS file:
Empire Burlesque Bob Dylan
USA
Columbia
10.90
1985
Hide your heart Bonnie Tyler
UK
CBS
Records
9.90
1988
Additional Info:
•
•
XML data can be embedded directly in an HTML page
XML combined with ASP can perform very well to—
– Manipulate databases.
– Manipulate the W3C Document object model
– Dynamically create XML documents from a web page.
Why mark up the data this way?
•
•
•
An XML file is in pure text
– any application should be able to use it.
– Increases data readability significantly.
Data transfer is easier
– E.g. An XML file containing information about books for sale can be
transferred to different sections of the pub. Company. Any platform can be
made to understand this file, and manipulate it.
Gives data some abstraction.
– Since the tags themselves carry some meaning, the data is somewhat
abstracted here.
– Displaying mechanisms do not clutter the algorithm. Style sheets (CSS),
XSL or data binding can take care of display.
– Much easier to change the look of the web pages, since the only thing that
needs to be changed is the style-sheets.
Works Cited:
•
•
•
Beginning Active Server Pages 3.0
Wrox, 2000. Pages 820-885
www.devguru.com
www.w3schools.com
Download