XPath at Work

We've already put XPath to work in Internet Explorer in an example at the beginning of this chapter. That example used XSLT to extract the names of the three planets in ch01_01.xml , as you saw in Figure 1.1. XSLT is a big use of XPath (and we're going to take a look at some basic XSLT next so that we can use it with XPath throughout the book), but some software packages let you use XPath directly.

The XPath Visualiser

The XPath Visualiser by Dimitre Novachev is a great tool that lets you see which nodes an XPath expression would select in an XML document. You can get the XPath Visualiser for free at http://www.vbxml.com/xpathvisualizer/.

After unzipping the download file, you use Internet Explorer to navigate to XPathMain.htm , opening this XPath tool as you see in Figure 1.2. You can use the Browse button in this tool to load an XML document, and enter the XPath expression you want to test into the "XPath Expression" box. In this example, we'll use our XML document ch01_01.xml that holds information about various planets, and test out the XPath expression //planet , which should find all the <planet> elements in this document. As you can see when you click the Select Nodes button, XPath Visualiser does indeed find all <planet> elements, as shown in Figure 1.2.

Figure 1.2. Using XPath Visualiser to apply an XPath expression.

graphics/01fig02.jpg

XPath Visualiser is a great tool that we'll see throughout our discussion of XPath 1.0.

The Interactive Expression Builder

The Interactive Expression Builder is an online tool that lets you test your XPath expressions, and you'll find it at http://www.develop.com/books/essentialxml/samples/xpath-builder/default.htm. You can see this tool at work in Figure 1.3.

Figure 1.3. Using the Interactive Expression Builder.

graphics/01fig03.jpg

As with the XPath Visualiser, you can enter the XPath expression you want to test into the Interactive Expression Builder. The problem here is that you can't use the Interactive Expression Builder to load any but the sample document you see in Figure 1.3 (there is a text field for loading other documents at the very bottom of this page, but it only generates JavaScript errors).

Still, if you're comfortable using the sample XML document that comes loaded into the Interactive Expression Builder, you can test your XPath expressions using that document, as you see in Figure 1.3, where we're selecting all <LineItem> elements.

XPath and .NET

The Microsoft .NET initiative uses a great deal of XML behind the scenes, and there's some XPath support built into .NET. You can see a few XPath .NET examples online at http://samples.gotdotnet.com/quickstart/howto. The current URL for the XPath examples is http://samples.gotdotnet.com/quickstart/howto/doc/xml/overviewofxml.aspx (if that URL has been changed by the time you read this, go to http://samples.gotdotnet.com/quickstart/howto and search for XML examples).

There are two .NET XPath examples at this site, and they both use this XML file, books.xml :

 
 <?xml version="1.0" ?> <!--  This file represents a fragment of a book store inventory database --> <bookstore>     <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">         <title>The Autobiography of Benjamin Franklin</title>         <author>             <first-name>Benjamin</first-name>             <last-name>Franklin</last-name>         </author>         <price>8.99</price>     </book>     <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">         <title>The Confidence Man</title>         <author>            <first-name>Herman</first-name>             <last-name>Melville</last-name>         </author>         <price>11.99</price>     </book>     <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">         <title>The Gorgias</title>         <author>             <name>Plato</name>         </author>         <price>9.99</price>     </book> </bookstore> 

To run the examples, click the "Query XML with an XPath Expression" and "Navigate with XPathNavigator" links under the heading "XPath and XSL Transformations" on the left of the page at http://samples.gotdotnet.com/quickstart/howto/doc/xml/overviewofxml.aspx. You can see the first example, which queries books.xml with XPath, at work in Figure 1.4 (its full URL is http://samples.gotdotnet.com/quickstart/howto/samples/Xml/QueryXmlDocumentXPath/VB/QueryXmlDocumentXPath.aspx).

Figure 1.4. Using XPath in .NET.

graphics/01fig04.jpg

When you click the Run button, the example applies various XPath expressions to books.xml , as you can see in the results in Figure 1.4.

The XPath Explorer

Besides .NET, a number of XPath tools are available that are Java-based. To use these tools, you'll need to have Java installed; the current version as of this writing is 1.4, which you can download for free from http://java.sun.com/j2se/1.4/download.html. You'll also find installation instructions in the download.

A useful Java XPath tool is the XPath Explorer, XPE, from Purple Technologies. You can get this tool for free from http:// sourceforge .net/project/showfiles.php?group_id=54719&release_id=98894. XPE comes as a Java Archive (JAR) file, and you start it at the command prompt as follows . (This example assumes that the Java program java.exe , which is in the Java installation's bin directory, is in your system's path and that xpe.jar is in the current directory). Here we'll use the command-line prompt % to stand for either the Windows command-line prompt in a DOS window or the Unix command-line prompt:

 
 %java -jar xpe.jar 

You can enter the URL of the XML file to work with in XPE, as you see in Figure 1.5. For example, to load our ch01_01.xml file into XPE, you'd use a file URL something like file:/c:/xpath/samples/ch01/ch01_01.xml in Windows.

Figure 1.5. Using the XPath Explorer.

graphics/01fig05.jpg

Now you can enter the XPath expression you want to use, as you see in Figure 1.5, where we're executing the expression //planet on ch01_01.xml . XPE will show you the results immediately, as you can see in the figure.

The XPath Tester

The XPath Tester is another Java tool for use with XPath, and you can get it for free at http://www.fivesight.com/downloads/xpathtester.asp. To start the XPath Tester, you use this command line ( assuming java.exe is in your system's path and xpathtester_1_4_saxon.jar is in the current directory):

 
 %java -jar xpathtester_1_4_saxon.jar 

The XPath Tester displays a File Open dialog that lets you select the XML document to work with and then opens that document as you see in Figure 1.6.

Figure 1.6. The XPath Tester.

graphics/01fig06.jpg

Now you can enter the XPath expression you want to execute and press Enter to execute it, as you see in Figure 1.6. When you do, the XPath Tester will display the results, as you see in the figure.

The Xalan XPath Application

Another Java tool you can use to evaluate XPath expressions is a sample application that comes with the Xalan XSLT processor created by the Apache group . You can download Xalan at http://xml.apache.org/xalan-j/index.html.

The name of this example is ApplyXPath, and it's in the Java Archive file xalansamples.jar that comes with Xalan. We'll use this JAR file by first setting the Java classpath variable, which tells Java where to look for applications to run, to xalansamples.jar . Next, you can run the ApplyXPath application, passing it the name of the XML file to use and the XPath expression to evaluate, like this:

 
 %set classpath=xalansamples.jar %java ApplyXPath ch01_01.xml //planet 

You can see the results in Figure 1.7, where you see that the application displays the elements matching the XPath expression we're testing.

Figure 1.7. Using Xalan's ApplyXPath.

graphics/01fig07.jpg

We're not going to expect that you know Java or .NET in this book. But if you do happen to know Java, you can take a look at the source code for the ApplyXPath example to see how to use XPath in Java. For example, here's some Java code that applies the XPath expression //planet to ch01_01.xml , adapted from the ApplyXPath sample applicationthis code isn't difficult, and it shows the Java classes you use to work with XPath:

 
 InputSource in = new InputSource(new FileInputStream("ch01_01.xml")); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); Document doc = dfactory.newDocumentBuilder().parse(in); Transformer serializer = TransformerFactory.newInstance().newTransformer(); NodeIterator nl = XPathAPI.selectNodeIterator(doc, "//planet"); Node n; while ((n = nl.nextNode())!= null) {     if (isTextNode(n)) {         StringBuffer sb = new StringBuffer(n.getNodeValue());         for (Node nn = n.getNextSibling(); isTextNode(nn);             nn = nn.getNextSibling())         {             sb.append(nn.getNodeValue());         }         System.out.print(sb); } 

Besides the ones we've taken a look at, there are other XPath tools out there as well, such as the popular XML editor, XML Spy, which also includes an XPath tool. You can get XML Spy at http://www.xmlspy.com/download.html.



XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net