XQuery - Testing Saxon-B
(Page 2 of 5 )
Once you’ve installed the software, you’ll need to make sure everything is working properly. To do this, create an XML document and an XQuery, and then use Saxon-B to retrieve information from the XML document.
Here’s the XML document that you’ll use to learn XQuery. Write this document using an editor and save it in the saxon directory as catalog.xml:
<?xml version="1.0"? >
<catalog>
<cd upc="602498678299">
<artist>U2</artist>
<title>How to Dismantle an Atomic Bomb</title>
<price>13.98</price>
<label>Interscope Records</label>
<date>2004-11-23</date>
</cd>
<cd upc="75679244222">
<artist>Led Zeppelin</artist>
<title>Physical Graffiti</title>
<price>22.99</price>
<label>Atlantic</label>
<date>1994-08-16</date>
</cd>
<cd upc="75678367229">
<artist>Rush</artist>
<title>Rush in Rio</title>
<price>13.98</price>
<label>Atlantic</label>
<date>2003-10-21</date>
</cd>
<cd upc="74646938720">
<artist>Billy Joel</artist>
<title>Songs in the Attic</title>
<price>10.99</price>
<label>Sony</label>
<date>1998-10-20</date>
</cd>
<cd upc="75678263927">
<artist>Led Zeppelin</artist>
<title>Houses of the Holy</title>
<price>10.98</price>
<label>Atlantic</label>
<date>1994-07-19</date>
</cd>
<cd upc="8811160227">
<artist>Jimi Hendrix</artist>
<title>Are You Experienced?</title>
<price>12.99</price>
<label>Experience Hendrix</label>
<date>1997-04-22</date>
</cd>
<cd upc="74640890529">
<artist>Bob Dylan</artist>
<title>The Times They Are A-Changin'</title>
<price>9.99</price>
<label>Sony</label>
<date>1990-10-25</date>
</cd>
</catalog>
Next, you’ll need to create the XQuery. Type the following XQuery into your editor and save it in the saxon directory in a file called catalog.xq. This XQuery retrieves and displays a list of titles contained in the XML document. Although the XQuery probably looks strange to you, you’ll understand each line of the XQuery by the time you finish reading this chapter.
<html>
<body>
List of titles in this catalog:
<br/>
<ul>
{
for $x in doc("catalog.xml")/catalog/cd/title
order by $x
return <li>{data($x)}</li>
}
</ul>
</body>
</html>
The final step to test Saxon-B is to execute the XQuery. Here’s what you need to do:
- Open a Command Prompt window if you’re using a Windows computer.
- Make saxon the current directory.
- Type the following command:
c:\Saxon> c:\jdk15\bin\java -cp saxon8.jar net.sf.saxon.Query -t catalog
.xq > output.html
- Press ENTER.
This command probably looks like a bunch of gibberish. It isn’t. The first part (c:\jdk15\bin\java) specifies the path to Java and runs Java. The second part (-cp saxon8.jar net.sf.saxon.Query) tells Java to extract the Query portion of the saxon8 .jar file, which is the file that contains Saxon-B. The third part (-t catalog.xq) of the command identifies the XQuery file, which is catalog.xq. The last part (> output .html) redirects the result of running the XQuery to the file called output.html.
TIP The jdk15 portion of the first part of the command is the directory where Java is installed on our computer. You probably installed Java in a different directory so you’ll need to replace jdk15 with the name of the directory on your computer where you installed Java.
You can use this same command to run all the examples in this chapter; however, for each example you’ll need to change the name of the query from catalog.xq to the name we give to the query.
Nothing much happens when you run Saxon-B—at least nothing you can see on the screen. Open the output.html in an editor and you’ll see the result of your XQuery. It should look like this:
<html>
<body>
List of titles in this catalog:<br><ul>
<li>Are You Experienced?</li>
<li>Houses of the Holy</li>
<li>How to Dismantle an Atomic Bomb</li>
<li>Physical Graffiti</li>
<li>Rush in Rio</li>
<li>Songs in the Attic</li>
<li>The Times They Are A-Changin'</li>
</ul>
</body>
</html> Figure 9-1 shows the result of your XQuery when it’s displayed by a browser.
Figure 9-1 Your browser displays the result of your XQuery request.
Next: How XQuery Works >>
More XML Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter nine of the book XML DeMYSTiFieD, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109). Check it out today at your favorite bookstore. Buy this book now.
|
|