Help:COAP-3180/hw4: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
 
(25 intermediate revisions by the same user not shown)
Line 12: Line 12:


'''Due''': Monday Week 6
'''Due''': Monday Week 6
Note: '''Programmers''' who wish to play with their own XML database can download [http://sourceforge.net/project/showfiles.php?group_id=17691 eXist] from source forge. Get the latest 1.1.x-newcore version, e.g.
eXist-1.1.1-newcore-build4311.jar.
To install it, type in a command window:
java -jar eXist-1.1.1-newcore-build4311.jar


=== Part 2 ===
=== Part 2 ===


To be announced...
* http://tecfax.unige.ch:8080/exist/ (The teacher's eXist server)
* Add an XML file to the //db/coap/coap3180 collection
 
; Variant A (ok for non programmers)
* Create at least two XQueries that can return some kind of HTML
* Submit them with a REST GET request
* Keep these URLs in a file and hand them over to the instructor
 
; Variant B
* Create a PHP script that uses the REST API or some other library (but this may turn out to be more work than you'd expect)
* Return a result in html (e.g. via php code, with a server-side XSLT stylesheet).
 
'''Due:''' Monday, week 7. Note: Since XML databases are relatively new technology and require Java programming knowledge for real life projects, I kept this homework as easy as I could....


== Classroom activities ==
== Classroom activities ==


=== Open an XML file in an XML editor ===
=== Use Xquery with an XML editor ===


; Learning how to use the XML Exchanger editor:
* Launch XML Exchanger lite.
* Launch XML Exchanger lite.
** In the left panel open all the files under xquery
** In the left panel open all the files under xquery
Line 27: Line 46:
** Note: You can find the xq file somewhere near the same place  
** Note: You can find the xq file somewhere near the same place  
* Repair the result file. There are some useless tags.
* Repair the result file. There are some useless tags.
; Other XML examples:


* Other examples files can be found on the N drive
* Other examples files can be found on the N drive
Line 37: Line 58:
* http://tecfa.unige.ch/guides/xml/examples/shakespeare.1.10.xml/ (various Shakespeare texts)
* http://tecfa.unige.ch/guides/xml/examples/shakespeare.1.10.xml/ (various Shakespeare texts)
* http://tecfa.unige.ch/proj/seed/catalog/net/xml/catalog-eng.xml (700k DITA document)
* http://tecfa.unige.ch/proj/seed/catalog/net/xml/catalog-eng.xml (700k DITA document)
=== Become somewhat familiar with eXist and its REST interface ===
Instructor's Web site:  http://tecfax.unige.ch:8080/exist/
(Official exist web site: http://exist.sourceforge.net/index.html)
See also: [http://en.wikibooks.org/wiki/XQuery/eXist_Crib_sheet eXist Crib sheet] (part of the [http://en.wikibooks.org/wiki/XQuery XQuery Wiki book]
* Note: To play with these examples your firewall must not block port 8080 !
* Launch the Java Webstart client (username = coap, password will be announced)
* Add a document to the //db/coap/coap3180 collection
* Retrieve some information with the REST interface and return it with some kind of html (browser must display it)
; XPath Examples (make sure to retrieve the whole lines !)
See the slides (not distributed with an URL, since not good enough ... get them from the N: drive
Simple XPath query:
<pre>
http://tecfax.unige.ch:8080/exist/rest/db/shakespeare?_query=//SPEECH[SPEAKER=%22JULIET%22]&_start=1&_howmany=10
http://demo.exist-db.org/rest/db/shakespeare?_query=//SPEECH[SPEAKER=%22JULIET%22]&_start=1&_howmany=10
</pre>
; XQuery examples
Xquery returning an XML fragment (using the full URL path):
<pre>
http://tecfax.unige.ch:8080/exist/rest/db/coap/cooking/dolores.xml?_query=for $t in //recipe let $name := $t//recipe_name return <name>{$name/text()} </name>
</pre>
Xquery returning an XML fragment (alternative version): [http://tecfax.unige.ch:8080/exist/rest/db/?_query=for%20$t%20in%20document(%22/db/coap/cooking/dolores.xml%22)//recipe%20let%20$name%20:=%20$t//recipe_name%20return%20%3Cname%3E{$name/text()}%20%3C/name%3E  click]
<pre>
http://tecfax.unige.ch:8080/exist/rest/db/?_query=for $t in document("/db/coap/cooking/dolores.xml")//recipe let $name := $t//recipe_name return <name>{$name/text()} </name>
</pre>
; Creating HTML with XQuery
Xquery returning some kind of HTML: [http://tecfax.unige.ch:8080/exist/rest/db/?_wrap=no&_query=%3Chtml%3E%20%3Cbody%3EList%20of%20names:%20%3Cul%3E%20{%20for%20$t%20in%20document(%22/db/coap/cooking/dolores.xml%22)//recipe%20let%20$name%20:=%20$t//recipe_name%20return%20%3Cli%3E{$name/text()}%20%3C/li%3E%20}%20%3C/ul%3E%3C/body%3E%3C/html%3E  Click]
<pre>
http://tecfax.unige.ch:8080/exist/rest/db/?_wrap=no&_query=<html> <body>List of names: <ul> { for $t in document("/db/coap/cooking/dolores.xml")//recipe let $name := $t//recipe_name return <li>{$name/text()} </li> } </ul></body></html>
</pre>
=== Same example in the database ===
http://tecfax.unige.ch:8080/exist/rest/db/coap/cooking/dolores.xq
<pre>
xquery version "1.0";
<html> <body>List of names:
<ul>
{ for $t in document("/db/coap/cooking/dolores.xml")//recipe
  let $name := $t//recipe_name
  return <li>{$name/text()} </li> }
</ul></body></html>
</pre>
=== Applying a XSLT stylesheet to a simple REST Query ===
By default, eXist will apply a stylesheet attached to the document to the result (therefore you'd have to modify it to cope will all sorts of query results). Alternatively you also can upload a xslt file to eXist or use one that sits on an other server:
<pre>
http://tecfax.unige.ch:8080/exist/rest/db/coap/cooking/dolores2.xml?_xsl=/db/coap/cooking/result.xsl&_query=for $t in //recipe let $name := $t//recipe_name return <name>{$name/text()} </name>
</pre>
Here is the code for the stylesheet "result.xsl". Note that we also declared the exist namespace, since by default eXist will wrap query results in exist:result tags
<pre>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:exist="http://exist.sourceforge.net/NS/exist"
                exclude-result-prefixes="exist">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
          <head><title>Query results</title></head>
            <body bgcolor="#FFFFFF">
              <h1>Query results</h1>
                <ul>
                    <xsl:apply-templates/>
                </ul>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="//name">
        <li>
            <xsl:apply-templates/>
        </li>
    </xsl:template>
</xsl:stylesheet>
</pre>
=== XHTML serialization ===
The following example file is inserted into the eXist database. You simply can call it like this:
http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.xq"
Instead of applying a stylesheet, you also may directly produce XHTML. One part of the XQuery/XSLT 2.0 standard allows tools to define extensions to do so.
<pre>
xquery version "1.0";
declare option exist:serialize "method=xhtml omit-xml-declaration=no indent=yes encoding=iso-8859-1
        doctype-public=-//W3C//DTD&#160;XHTML&#160;1.0&#160;Transitional//EN
        doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'";
(: Retrieve some canned stuff from an XML file in the db :)
<html>
<head>
  <title>List of directors</title>
</head>
<body>
<h1>List of directors</h1>
<ul>
  {  for $realisateur in document("/db/test-xquery/dvd.xml")/dvd/fiche/Realisateur
  return
    <li> Name: { $realisateur/text() } </li>
}
</ul>
Note: This xq file can be called with the URL:
<a href="http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.xq">http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.xq </a>
<p>
Source: <a href="http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.text">http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.text</a>
</p>
</body>
</html>
</pre>
=== On using XForms ===
See: http://en.wikibooks.org/wiki/XForms/eXist
=== XQuery Sandboxes on the web ===
* [http://www.orbeon.com/ops/sandbox-transformations/xquery/ Orbeon Xquery sandbox]
* [http://demo.exist-db.org/sandbox/sandbox.xql XQuery sandbox at eXist]
* Else, do these transforms with XML Exchanger light


== Reading and links ==
== Reading and links ==
Line 44: Line 194:
'''Reading'''
'''Reading'''
* Ronald Bourret (2005). XML and Databases, http://www.rpbourret.com/xml/XMLAndDatabases.htm
* Ronald Bourret (2005). XML and Databases, http://www.rpbourret.com/xml/XMLAndDatabases.htm
* Darshan Singh, Essential XQuery - The XML Query Language, http://www.yukonxml.com/articles/xquery/
* Darshan Singh, Essential XQuery - The XML Query Language, http://www.yukonxml.com/articles/xquery/ (skip the part about functions)
* XQuery http://en.wikipedia.org/wiki/XQuery (Wikipedia)
* XQuery http://en.wikipedia.org/wiki/XQuery (Wikipedia)


=== Reference ===
=== Reference ===


Best XQuery manual/tutorial
* [http://en.wikibooks.org/wiki/XQuery XQuery Wiki book]
You can find additional links and more tutorials in this wiki
* [[XQuery]]
* [[XQuery_tutorial - basics]] (no tutorial so far, just links)
* [[XML database]]


=== Slides ===
=== Slides ===


* http://tecfa.unige.ch/guides/te/files/xml-databases.pdf
* http://tecfa.unige.ch/guides/te/files/xml-databases.pdf
* http://tecfa.unige.ch/guides/te/files/connectivity-web-services.pdf


;Prerequisites and catchup (if needed)
;Prerequisites and catchup (if needed)
Line 64: Line 223:
* http://tecfa.unige.ch/guides/te/files/xml-schema.pdf (Creating XML Schemas)
* http://tecfa.unige.ch/guides/te/files/xml-schema.pdf (Creating XML Schemas)
* http://tecfa.unige.ch/guides/te/files/xml-ns.pdf (XML Namespaces)
* http://tecfa.unige.ch/guides/te/files/xml-ns.pdf (XML Namespaces)
== SVG demo ==
(if there is time left)
See the [[SVG]] article and [[SVG links]]

Latest revision as of 00:05, 10 December 2007

Homework 4 - COAP 3180 Fall 2 2007

COAP 3180 Course Page

Summary

  • Write some Xquery code to retrieve data from an XML collection

Part 1

  • Find somewhere or create a file with some XML data (at least 1 page long)
  • Write at least two different XQuery expressions to retrieve data from this file

Due: Monday Week 6

Note: Programmers who wish to play with their own XML database can download eXist from source forge. Get the latest 1.1.x-newcore version, e.g. eXist-1.1.1-newcore-build4311.jar.

To install it, type in a command window: java -jar eXist-1.1.1-newcore-build4311.jar

Part 2

Variant A (ok for non programmers)
  • Create at least two XQueries that can return some kind of HTML
  • Submit them with a REST GET request
  • Keep these URLs in a file and hand them over to the instructor
Variant B
  • Create a PHP script that uses the REST API or some other library (but this may turn out to be more work than you'd expect)
  • Return a result in html (e.g. via php code, with a server-side XSLT stylesheet).

Due: Monday, week 7. Note: Since XML databases are relatively new technology and require Java programming knowledge for real life projects, I kept this homework as easy as I could....

Classroom activities

Use Xquery with an XML editor

Learning how to use the XML Exchanger editor
  • Launch XML Exchanger lite.
    • In the left panel open all the files under xquery
    • Click on the bottom tab with the books.xml file
    • In the menubar select Transform->XQuery
    • Note: You can find the xq file somewhere near the same place
  • Repair the result file. There are some useless tags.
Other XML examples
  • Other examples files can be found on the N drive

Files from Essential XQuery - The XML Query Language

Also some more ambitious

Become somewhat familiar with eXist and its REST interface

Instructor's Web site: http://tecfax.unige.ch:8080/exist/ (Official exist web site: http://exist.sourceforge.net/index.html)

See also: eXist Crib sheet (part of the XQuery Wiki book

  • Note: To play with these examples your firewall must not block port 8080 !
  • Launch the Java Webstart client (username = coap, password will be announced)
  • Add a document to the //db/coap/coap3180 collection
  • Retrieve some information with the REST interface and return it with some kind of html (browser must display it)
XPath Examples (make sure to retrieve the whole lines !)

See the slides (not distributed with an URL, since not good enough ... get them from the N: drive

Simple XPath query:

 http://tecfax.unige.ch:8080/exist/rest/db/shakespeare?_query=//SPEECH[SPEAKER=%22JULIET%22]&_start=1&_howmany=10

http://demo.exist-db.org/rest/db/shakespeare?_query=//SPEECH[SPEAKER=%22JULIET%22]&_start=1&_howmany=10
XQuery examples

Xquery returning an XML fragment (using the full URL path):

http://tecfax.unige.ch:8080/exist/rest/db/coap/cooking/dolores.xml?_query=for $t in //recipe let $name := $t//recipe_name return <name>{$name/text()} </name>

Xquery returning an XML fragment (alternative version): click

http://tecfax.unige.ch:8080/exist/rest/db/?_query=for $t in document("/db/coap/cooking/dolores.xml")//recipe let $name := $t//recipe_name return <name>{$name/text()} </name>
Creating HTML with XQuery

Xquery returning some kind of HTML: Click

http://tecfax.unige.ch:8080/exist/rest/db/?_wrap=no&_query=<html> <body>List of names: <ul> { for $t in document("/db/coap/cooking/dolores.xml")//recipe let $name := $t//recipe_name return <li>{$name/text()} </li> } </ul></body></html>

Same example in the database

http://tecfax.unige.ch:8080/exist/rest/db/coap/cooking/dolores.xq

xquery version "1.0";
<html> <body>List of names: 
<ul> 
{ for $t in document("/db/coap/cooking/dolores.xml")//recipe
  let $name := $t//recipe_name 
  return <li>{$name/text()} </li> } 
</ul></body></html>

Applying a XSLT stylesheet to a simple REST Query

By default, eXist will apply a stylesheet attached to the document to the result (therefore you'd have to modify it to cope will all sorts of query results). Alternatively you also can upload a xslt file to eXist or use one that sits on an other server:

http://tecfax.unige.ch:8080/exist/rest/db/coap/cooking/dolores2.xml?_xsl=/db/coap/cooking/result.xsl&_query=for $t in //recipe let $name := $t//recipe_name return <name>{$name/text()} </name>

Here is the code for the stylesheet "result.xsl". Note that we also declared the exist namespace, since by default eXist will wrap query results in exist:result tags

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
                xmlns:exist="http://exist.sourceforge.net/NS/exist"
                exclude-result-prefixes="exist">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
          <head><title>Query results</title></head>
            <body bgcolor="#FFFFFF">
              <h1>Query results</h1>
                <ul>
                    <xsl:apply-templates/>
                </ul>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="//name">
        <li>
            <xsl:apply-templates/>
        </li>
    </xsl:template>
</xsl:stylesheet>

XHTML serialization

The following example file is inserted into the eXist database. You simply can call it like this: http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.xq"

Instead of applying a stylesheet, you also may directly produce XHTML. One part of the XQuery/XSLT 2.0 standard allows tools to define extensions to do so.

xquery version "1.0";
declare option exist:serialize "method=xhtml omit-xml-declaration=no indent=yes encoding=iso-8859-1
        doctype-public=-//W3C//DTD XHTML 1.0 Transitional//EN
        doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'";
(: Retrieve some canned stuff from an XML file in the db :)
<html>
<head>
  <title>List of directors</title>
</head>
<body>
<h1>List of directors</h1>
<ul>
   {   	for $realisateur in document("/db/test-xquery/dvd.xml")/dvd/fiche/Realisateur
   	return
   	   <li> Name: { $realisateur/text() } </li>
	}
</ul>
Note: This xq file can be called with the URL: 
<a href="http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.xq">http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.xq </a>
<p>
Source: <a href="http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.text">http://tecfax.unige.ch:8080/exist/rest/db/test-xquery/dvd-list.text</a>
</p>
</body>
</html>

On using XForms

See: http://en.wikibooks.org/wiki/XForms/eXist

XQuery Sandboxes on the web

Reading and links

Reading

Reading

Reference

Best XQuery manual/tutorial

You can find additional links and more tutorials in this wiki

Slides

Prerequisites and catchup (if needed)

SVG demo

(if there is time left)

See the SVG article and SVG links