Sady Home

Note my coding life

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

What is XML?

  • XML stands for EXtensible Markup Language
  • XML is a markup language much like HTML
  • XML was designed to describe data
  • XML tags are not predefined. 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
  • XML is a W3C Recommendation

XML was designed to describe, carry data and to focus on what data is.
HTML was designed to display data and to focus on how data looks.
XML can Separate Data from HTML
XML is Used to Exchange, Share and Store Data
XML documents use a self-describing and simple syntax.
XML Elements Must Have a Closing Tag
XML Tags are Case Sensitive
XML Elements Must be Properly Nested
XML Documents Must Have a Root Element
XML Attribute Values Must be Quoted (" or ')
With XML, White Space is Preserved
With XML, carriage return (CR) / line feed (LF) is Converted to LF

XML elements must follow these naming rules:

  • Names can contain letters, numbers, and other characters
  • Names must not start with a number or punctuation character
  • Names must not start with the letters xml (or XML, or Xml, etc)
  • Names cannot contain spaces
Displaying XML with CSS
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>

Displaying XML with XSL
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>

XML Data Embedded in HTML
<xml id="note" src="note.xml"></xml>

Bind Data Island to HTML Elements
<xml id="cdcat" src="cd_catalog.xml"></xml>
<table border="1" datasrc="#cdcat">
<tr>
<td><span datafld="ARTIST"></span></td>
<td><span datafld="TITLE"></span></td>
</tr>
</table>

XML Parser
<head>
<script type="text/javascript">
var xmlDoc;
function loadXML()
{
// code for IE
if (window.ActiveXObject)
  
{
  xmlDoc
=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async
=false;
  xmlDoc.load(
"note.xml");
  getmessage();
  }

// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  
{
  xmlDoc
=document.implementation.createDocument("","",null);
  xmlDoc.load(
"note.xml");
  xmlDoc.onload
=getmessage;
  }

else
  
{
  alert('Your browser cannot handle 
this script');
  }

}
function getmessage()
{
document.getElementById(
"to").innerHTML=
xmlDoc.getElementsByTagName(
"to")[0].childNodes[0].nodeValue;
}

</script>
</head>
<body onload="loadXML()">
<span id="to"></span>
</body>

Solving Name Conflicts Using a Prefix or Using Namespaces
<h:table></h:table><f:table></f:table>
<h:table xmlns:h="http://www.w3.org/TR/html4/"></h:table>
<f:table xmlns:f="http://www.w3schools.com/furniture"></f:table>

Escape Characters
Only the characters "<" and "&" are strictly illegal in XML.

Everything inside a CDATA section is ignored by the parser.
<![CDATA[    ]]>

The XMLHttpRequest Object Reference

Method Description
abort() Cancels the current request
getAllResponseHeaders() Returns the complete set of http headers as a string
getResponseHeader("headername") Returns the value of the specified http header
open("method","URL",async,"uname","pswd") Specifies the method, URL, and other optional attributes of a request

The method parameter can have a value of "GET", "POST", or "PUT" (use "GET" when requesting data and use "POST" when sending data (especially if the length of the data is greater than 512 bytes.

The URL parameter may be either a relative or complete URL.

The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing

send(content) Sends the request
setRequestHeader("label","value") Adds a label/value pair to the http header to be sent

Property Description
onreadystatechange An event handler for an event that fires at every state change
readyState Returns the state of the object:

0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete

responseText Returns the response as a string
responseXML Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties
status Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK")
statusText Returns the status as a string (e.g. "Not Found" or "OK")
 

What is XPath?

  • XPath is a syntax for defining parts of an XML document
  • XPath uses path expressions to navigate in XML documents
  • XPath contains a library of standard functions
  • XPath is a major element in XSLT
  • XPath is a W3C Standard

Expression Description
nodename Selects all child nodes of the named node
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind
| Selecting Several Paths

Example
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00
//title[@lang='eng'] Selects all the title elements that have an attribute named lang with a value of 'eng'

* Reference to W3C Schools

posted on 2007-10-23 11:22  Sady  阅读(267)  评论(0)    收藏  举报
凭飞堂