7. Describing Data with XML
<BusinessCard>
<name>Joe Marini</name>
<phone type = "home">+1 (415) 555-1234</phone>
<phone type = "work">+1 (415) 555-2222</phone>
<email>joe@gmail.com</email>
</BusinessCard>
8. Advantages and drawabacks of XML
Advantages
- XML keeps content separate from presentation
- XML is an open format that can be read by many applications
- XML can be used on both the client and the server
- XML has widespread support in multiple languages, runtimes
- XML makes it possible for disparate systems to exchange data
Drawbacks
- XML is not suitable for a very large data sets (like 1GB)
- Some formats like JSON might be better in some cases
- Some types like images aren’t represented well
- XML can quickly get hard to read when complex
9. Real world examples of XML
- RSS pages
- Microsoft Office can aslo support XML
10. Types of XML content
- XML document declaration (optional but recommended)
standalone
: whether the document is complete by itself
<?xml version="1.0" encoding="UTF-8" standalone="yes">
-
Elements (Tags) and Attributes
attribute
appears only once in the opening element
<element attribute="value">
- Comments
<!-- This is a comment -->
-
Character Data
- It’s part of document but not parsed by XML parser. Typically used to contain unescaped textual data
<![CDATA[This is text & data]]>
<!-- Example -->
<script>
<![CDATA[
function (a,b) {
return a < b;
}
]]>
</script>
- Processing Instructions
- They are special instructions to the XML parser.
<?SpellCheckMode mode="us-english"?>
- Entity References
- Help shorten and modularize XML documents
- Provide markup for otherwise illegal characters
- General entities:
- Replaced by parser with a full string
- Example:
©right
; or&author
- Character entities if the character is not on the keyboard
<
;
Character (<) and General (©right)
11. Proper XML syntax
- XML documents must have a single root tag
- XML documents must be “well-formed”
- Empty tags must be closed with
/>
- Instead of
<elem> </elem>
, use<elem/>
- Instead of
- Attriute values can’t be minimized
<option selected>
is illegal, use<option selected="selected">
- Attribute values must be inside of quotes, single or double
<elem attr=val>
is illegal, use<elem attr="val">
12. Valid XML documents
INstead of only checking whether the the XML document is “well-formed” or not, we can also apply some rules to the parser like “tag A must be inside the tag B” and so on. If the document passes the rules and that’s called valid.
There are two ways to do this:
- Document type definition (DTD)
- simple to use but not powerful
- written using a syntax that’s different that XML
- XML Schema
- more powerful and flexible that DTD
13. XML namespaces
Form
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- Example -->
<table>
<tr>
<td>Cell Content</td>
</tr>
<tr>
<td>Cell Content</td>
</tr>
</table>
<table>
<type>Coffee</type>
<price>199.99</price>
<material>Wood</material>
<stock>25</stock>
</table>
<!-- Solution -->
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:furn="http://www.funitur.org/items">
<table>
<tr><td>
<furn:table>
<furn:type>Coffee</furn:type>
<furn:price>199.99</furn:price>
<furn:material>Wood</furn:material>
<furn:stock>25</furn:stock>
</furn:talbe>
</td></tr>
</table>
- It’s usually a URL but not necessary, and it needs to be unique
- Prevent tags from different languages from conflicting