XML Notes
- Definition
- Extensible Markup Language used as a complement to HTML
- Designed to structure, store and send data
- No predefined tags, you must define your tags!
- Stored in plain text files
- Use
- Used to store data in files external to the HTML page
- Used to store data inside HTML pages as data islands
- Used to exchange data between incompatable systems
- Used to share data
- Used to create new languages ie:
WML tutorial
- excludes the need for data conversion between applications
- Example
<?xml version = "1.0" encoding = "ISO-8859-1"?>
<NOTE>
  <TO>bill</TO>
  <FROM>bob</FROM>
  <HEADING>Reminder</HEADING>
  <BODY>Don't forget me this weekend!</BODY>
</NOTE>
- The first line is always the XML declaration
- The second line describes the root element of the document
- The next 4 lines describe child elements of the root
- The last line defines the end of the root element
Syntax
- All XML elements must have a closing tag
- XML tags are case sensitive
- All XML elements must be properly nested
- All XML documents must have a root element
- All child elements must be within this root element
- Attribute values must always be quoted
- White space is not truncated
- A new line is always stored as LF
- <!-- This is a comment in XML and HTML -->
Elements
- XML elements are extensible, so you can add onto them later
- XML elements have relationships as parents and children
- XML elements have simple naming rules
- Elements have content
- Element content
- Mixed content
- Simple or text content
- Empty content
- Element names can contain letters, numbers and other characters
- Element names must not start with a number or punctuation character
- Element names must not start with the letters XML, xml or Xml
- Element names cannot contain spaces
Attributes
- XML elements can have attributes in the start tag like HTML
- Attributes often provide information that is not part of the data
- Attributes can use either single or double quotes
- if the attributes encloses a double quoted item... use single quotes
- <gangster name = 'George "Shotgun" Zeigler'>
- if the attributes encloses a single quoted item... use double quotes
- <gangster name = "George 'Shotgun' Zeigler">
- Data can be stored in child elements or in attributes
- Attributes cannot contain multiple values, child elements can
- Attributes are not easily expandable
- Attributes cannot describe structures, child elements can
- Attributes are more difficult to manipulate by program code
- Attribute values are not easy to validate in a document type definition
Validation
- XML validated against a DTD is Valid XML
- Well formed XML documents have correct syntax and conform to a DTD
- A DTD defines the legal elements of an XML document
- XML Schema is an XML based alternative to DTD
- Validation errors will halt the XML processing program
Browsers
- I.E. Supports
- Viewing of XML documents
- Full support for W3C DTD standards
- XML embedded in HTML as data islands
- Binding XML data to HTML elements
- Transforming and displaying XML with XSL
- Displaying XML with CSS
- Access to the XML DOM
- I.E. also has support for Behaviors
- Behaviors is a Microsoft only technology
- Behaviors can separate scripts from an HTML page
- Behaviors can store XML data on the client's disk
Viewing XML Files
- Open it with I.E.
- Browser will report errors
- XML documents do not carry information specifying display
Displaying XML with CSS
- With CSS you can add display information to an XML document
- <?xml-stylesheet type = "text/css" href="simple.css"?>
- Use XHTML to create XML pages
Displaying XML with XSL: Extensible Style Sheet Language
- XSL is the preferred style sheet language of XML
- <?xml-stylesheet type = "text/xsl" href="simple.xsl">
- Links the XML file to the XSL file
Data Islands
- XML data can be embedded in HTML using the <XML> tag
- Example:
- <XML Id = "note">
-   <to>Bob</to>
-   <from>Bill</from>
-   <heading>Reminder</heading>
-   <body>Don't forget me this weekend!</body>
- </note>
- </XML>
- Data islands can also be bound to HTML elements
- They automatically load so no JavaScript or VBScript needed
- Example:
- <xml id="cdcat" src="Example 3.xml"></xml>
- <table align = center border = "1" datasrc="#cdcat">
-   <td><span datafld="ARTIST"></span></td>
-   <td><span datafld="TITLE"></span></td>
- </table>
| Artist | Title |
| This is my CD collection |
|
|
The Microsoft XML Parser
- Needed to read, update, create and manipulate an XML document during run
- Using the XML parser that comes with I.E.
- The parser is available to scripts both inside HTML documents and
- inside ASP files. The parser features a language-neutral programming
- model that supports:
- JavaScript, VBScript, Perl, VB, Java, C++ and more
- W3C XML 1.0 and XML DOM
- DTD and validation
- JavaScript
- var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
- VBScript
- set xmlDoc=CreateObject("Microsoft.XMLDOM")
- VBScript in ASP
- set xmldoc=Server.CreateObject("Microsoft.XMLDOM")
- Loading an XML file into the parser
- XML files can be loaded into the XML parser using script code
- Example:
- <SCRIPT type="text/javascript">
-   var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
-   xmlDoc.async="false"
-   xmlDoc.load("Example 4.xml")
-   // -------- processing the document goes here
- </SCRIPT>
- Loading Pure XML Text Into The Parser
- XML text can also be loaded from a text string
- Example:
- <SCRIPT type="text/javascript">
-   var text="<note>"
-   text = text+"<to>Bob</to><from>Bill</from>"
-   text = text+"<heding>Reminder</heding>"
-   text = text+"<body>Don't forget me this weekend!</body>"
-   text = text+"</note>"
-   var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
-   xmlDoc.async="false"
-   xmlDoc.loadXML(text)
-   // ------- processing goes here
- </SCRIPT>
- Displaying XML with JavaScript/VBScript
- Both can be used to import data from an XML file and display it in HTML
XML In Real Life
- The XML standard makes it easier to produce, receive and archive information
across different hardware, software and programming languages.
NameSpaces
- Provides a method to avoid element name conflicts
- Don't use HTML element names!
- Use a prefix
- <H:TABLE>
- Use a namespace attribute in the start tag of an element
- <H:TABLE XMLNS:H="/Files/XML/Work Out/Schedule"
- Syntax
- xmlns:namespace-prefix="namespace"
- The namespace should be a URI (Uniform Resource Identifier)
- All child elements with the same prefix are associated with the namespace
- The address used to identify the namespace is not used by the parser
to look up information. The only purpose is to give the namespace a
unique name however, very often companies use the namespace as a pointer
to a web site containing information about the namespace.
- Uniform Resource Identifiers
- (URI) A string of characters which identifies an internet resource.
- (URL) Identifies an internet domain address
- (URN) Universal Resource Name
- Default Namespaces
- Defining a default namespace for an element saves
having to use prefixes in the child elements
- <ELEMENT XMLNS="NameSpace">
CDATA
- Everything inside a CDATA section is ignored by the parser
- A CDATA section starts with "<![CDATA]" and ends with "]]>":
- Example:
- <SCRIPT>
-   <![CDATA[
-     function matchwo(a,b)
-     {
-       if (a < b && a < 0) then
-       {
-         return 1
-       }
-       else
-       {
-         return 0
-       }
-     }
-   ]]>
- </SCRIPT>
- A CDATA section cannot contain another CDATA section
- Illegal XML characters have to be replaced by entity references
- <MESSAGE>if salary < 100000 then</MESSAGE>
- 5 predefined entity references in XML
-
| &lt;
| <
| less than
|
| &gt;
| >
| greater than
|
| &amp;
| &
| ampersand
|
| &apos;
| '
| apostrophe
|
| &quot;
| "
| quotation mark
|
- Make sure there are no spaces or line breaks inside the "]]>" string
Encoding
- XML documents can contain foreign characters so save XML documents as Unicode
- Watch out for encoding errors!
Servers
- XML files can be generated on an internet server without any installed XML controls
- XML files can be stored on the server same way as HTML files
- XML can be generated on an ASP server without any installed XML software
- ASP file
- <#
- response.ContentType="text/xml"
- response.Write("<?xml version = '1.0' encoding='ISO-8859-1'?>")
- response.Write("<note>")
- respnose.Write("<from>Bob</from>")
- response.Write("<to>Bill</to>")
- response.Write("<message>Remember me this weekend</message>")
- response.Write("</note>")
- #>
- XML can be generated from a database without any installed XML software
- ASP file
- <#
- response.ContentType = "text/sml"
- set conn=Server.CreateObject("ADODB.Connection")
- conn.provider="Microsoft.Jet.OLEDB.4.0'"
- conn.open server.mappath("../ado/database.mdb")
- sql="select fname,lname from tblGuestBook"
- set rs=Conn.Execute(sql)
- rs.MoveFirst()
- response.write("<?xml version='1.0' encoding='ISO-8859-1'?>")
- response.write("<guestbook>")
- while (not rs.EOF)
-   response.write("<guest>")
-   response.write("<fname>" & rs("fname") & "</fname>")
-   response.write("<lname>" & rs("lname") & "</lname>")
-   response.write("</guest>")
-   rs.MoveNext()
- wend
- rs.close()
- conn.close
- response.write("</guestbook>")
- #>
Applications
- Start with an XML document
- Example 3.xml
- Load the document into a Data Island on a page
- <XML Src = "Example 3.xml" Id = "xmldso" Async = "false"></XML>
-
- Bind the Data Island to an HTML table
- <TABLE Datasrc="#xmldso" Width = 50% Border>
-   <THEAD>
-     <TH>Title</TH>
-     <TH>Artist</TH>
-     <TH>Year</TH>
-   </THEAD>
-   <TR Align = Left>
-     <TD><SPAN Datafld = "TITLE"></SPAN>
-     <TD><SPAN Datafld = "ARTIST"></SPAN>
-     <TD><SPAN Datafld = "YEAR"></SPAN>
-   </TR>
- </TABLE>
- <SPAN> or <DIV> elements can also be used to display XML data
- Adding a Navigation Script to the XML
- <SCRIPT Type = "Text/JavaScript">
-   function MoveNext()
-   {
-     x=xmldso.recordset
-     if (x.absoluteposition < x.recordcount)
-     {
-       x.movenext()
-     }
-   }
-   function moveprevious()
-   {
-     x=xmldso.recordset
-     if (x.absolutposition > 1)
-     {
-       x.moveprevious()
-     }
-   }
- </SCRIPT>
HTTP Requests using JavaScript or VBScript
- XML data can be requested from a server using an HTTP request
- var objHTTP = new ActiveXObject("Microsoft.XMLHTTP")
- objHTTP.Open('GET','httprequest.asp',false)
- ObjHTTP.Send()
- To view the above result
- document.all['A1'].innerText=objHTTP.status
- document.all['A2'].innerText=objHTTP.statusText
- document.all['A3'].innerText=objHTTP.responseText
-
- With HTTP Requests you communicate with the Server See
Example 5
Behaviors
- A Behavior is a CSS Attribute Selector that can point to an XML file
that contains code to be executed against elements in a web page
- A <STYLE> Method to completely remove script code from an HTML page
- Allows for script libraries and attaching to elements on multiple HTML pages
- A Microsoft only technology
- File extension = ".htc"
- Example:
- <STYLE> H1 ( Behavior:URL(Behave.htc) )</STYLE>
- The Behavior code for the Attribute is stored in an XML document Behave.htc
- <component>
-   <attach for="element event="onmouseover" handler="hig_lite" />
-   <attach for="element event="onmouseout"  handler="low_lite" />
-   <script type="text/javascript">
-     function hig_lite()
-     {
-       element.style.color=255
-     }
-     function low_lite()
-     {
-       element.style.color=0
-     }
-   </script>
- </component>
Related Technologies
- XHTML - Extensible HTML
- XHTML is the reformulation of HTML 4.01 in XML.
- CSS - Cascading Style sheets
- CSS style sheets can be added to XML document to provide display information
- XSL - Extensible Style Sheet Language
- XSL consists of 3 parts
- XSLT - XML document transformation
- Used to transform XML files into many different output formats
- XPath - a pattern matching syntax
- A language for addressing parts of an XML document
- Designed to be used by XSLT and XPointer
- A formatting Object Interpretation
-
- XLink - XML Linking Language
- Allows elements to be inserted into XML to create links between XML resources
- XPointer - XML Pointer Language
- Supports addressing into the internal structures of XML documents
- such as; elements, attributes and content
- DTD - Document Type Definition
- Can be used to define the legal building blocks of an XML document
- NameSpaces
- Defines a method for defining element and attribute names
- used in XML by associating them with URI references
- XSD - XML Schema
- Powerful alternatives to DTD's written in XML
- XDR - XML Data Reduced
- A reduced version of XML Schema
- DOM - Document Object Model
- Defines interfaces, properties and methods to manipulate XML documents
- XQL - XML Query Language
- Supports query facilities to extract data from XML documents
- SAX - Simple API for XML
- Interface to read and maniuplate XML documents