CompSci 316 (Fall 2012):
Introduction to Database Systems

Course Information
Lecture Notes
Assignments
Tentative Syllabus
Programming Notes
Getting Started with the dbcourse Virtual Machine
Getting Started with Gradiance
PostgreSQL Notes
Using ra
JDBC Notes
Tomcat Notes
Django Notes
PHP Notes
XML Notes
Homework/Project Submission
Sakai (Grades/Project Discussion)

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 in /home/dbcourse/examples/xml-zthes/.

To check that your XML document is well-formed (i.e., no synatx errors), use the command "xmllint --noout file", where file is the file name of the XML document. If the document is well-formed, no output is generated; otherwise, any error will be reported.

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

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

To check your XML document against an XSD (XML Schema) file, use the command "xs-check xsd_file xml_file", where xsd_file is the file name of the XML Schema document and xml_file is the file name of the XML document. If you omit xml_file, the command will simply check that the XML Schema is correct.


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 "xq 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 "xq -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 "xslt 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/. 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. XMLChecker.java illustrates how to check whether an XML document is well-formed, and furthermore, valid with respect to some DTD. Finally, XSDValidator.java shows how to validate an XML document against an XML Schema.

Since JAXP is part of the standard Java SDK, you can simply use the command "javac *.java" to compile your code in this directory. However, since the standard CLASSPATH does not include the current directory, you need to use the command "java -classpath . JavaClassName" to run the programs.

Last updated Wed Sep 12 21:58:01 EDT 2012