CPS 116 (Fall 2006):
Introduction to Database Systems

Course Information   Lecture Notes   Assignments   Tentative Syllabus   Programming Notes   Blackboard (Grades)
Getting Started with rack040   Getting Started with Gradiance   Using ra   DB2 SQL Notes   Tomcat Notes   XML Notes

XML Notes


Index


General information

Here is a collection of pointers to relevant standards and tutorials on the Web:


Writing and validating XML

You can use your favorite editor to edit XML documents. The file name should have suffix .xml. Emacs has a descent XML mode for editing XML, which is automatically invoked for file names with .xml suffix. There are also many specialized XML editors available, and some of them come with nice templates and automatic validation. Just search for "XML editor" in Google.

Some XML and DTD examples can be found on rack040 in /home/dbcourse/examples/xml-zthes/.

To check that your XML document is well-formed (i.e., no synatx errors), use the command xmlchecker file on rack040, where file is the file name of the XML document.

To validate your XML document against a DTD (you will need a <!DOCTYPE declaration in the XML document), use the command xmlchecker -v file. The additional -v flag turns on DTD validation.

Most of the Web browser these days also support visualization and validation of XML documents. Simply open the XML file from Firefox or Internet Explorer; it will automatically validate the XML document and display it in a nice format.


Using XPath, XQuery, and XSLT with Saxon

Saxon is an open-source XPath/XQuery/XSLT engine. To run an XQuery, use your favorite text editor to create a text file, say xqueryFile, containing the query string, and then issue the command saxonxq xqueryFile. The result will be printed to standard output. As an example, you may try a file containing the following XQuery string:

  <result>
    {
    for $i in doc("/home/dbcourse/examples/xml-xmark/auction.xml")//item
    where $i/payment = "Creditcard"
    return $i/name
    }
  </result>
Alternatively, you can specify the XML file on the command line with the command saxonxq -s /home/dbcourse/examples/xml-xmark/auction.xml xqueryFile; doing so allows the following XQuery (note that doc(...) is no longer needed):
  <result>
    {
    for $i in //item
    where $i/payment = "Creditcard"
    return $i/name
    }
  </result>

To run an XSLT stylesheet, use the command saxonxsl xmlFile xsltFile, where xmlFile specifies the name of the input XML file, and xsltFile specifies the name of the XSLT stylesheet file. The result will be output to standard output.


Coding with JAXP

Plenty of examples can be found in /home/dbcourse/examples/jaxp/ on rack040. SaxEcho.java uses an SAX parser to parse and validate an input XML document, and uses SAX API to echo the input to standard output. DomEcho.java uses a DOM parser to parse and validate an input XML document and to build an in-memory DOM representation of the input; it then uses DOM API to print the DOM tree out as XML. DomTree.java prints out the DOM tree representation directly, which can be useful in debugging. The standard way of converting DOM into XML for output is to use a default Transformer, which is illustrated in Stylizer.java. Stylizer.java also shows how to use Transformer to perform XSLT transformations.

Since JAXP is now part of the standard Java SDK, you can simply use javac *.java to compile your code. However, since the standard CLASSPATH does not include the current directory, you may need to use java -classpath . JavaClassName in the directory containing JavaClassName.class.

Last updated Thu Oct 19 00:53:28 EDT 2006