Appendix B: Setup for XPath Practice


If you wish to try out XPath, do as follows:

  1. Ensure that your machine has Java installed:

    1. Select Start > Run.

    2. Type cmd and press Enter.

    3. At the command prompt, type the following string and press Enter:

       C:\ java -version 

    4. If the command is unrecognized, continue with the next step; otherwise, continue with step 3.

  2. To download and install the Java J2SE Runtime Environment (JRE) 5.0, go to the following Web site and follow the directions there: http://java.sun.com/javase/downloads/index.jsp.

  3. As of this writing, you can get the required Xalan files (xalan-j_2_7_0- bin-2jars.zip) from the site http://www.axint.net/apache/xml/xalan-j. If that site is unavailable, go to the download site for the Apache Xalan project, http://xml.apache.org/xalan-j/downloads.html. From there, follow directions to download Xalan.

  4. After downloading the files, unzip them to a directory (for example, C:\Xalan).

  5. Set up the system variable CLASSPATH on your machine. On Windows XP, for example:

    1. At the desktop, double-click My Computer and select Control Panel.

    2. Select System.

    3. Select the Advanced tab and from there, Environment Variables.

    4. Under System Variables, click New; name a variable CLASSPATH, and, without line breaks, type a Variable value such as the following (substituting the directory names from your download, if appropriate):

       .;C:\Xalan\xalan-j_2_7_0\xalan.jar; C:\Xalan\xalan-j_2_7_0\serializer.jar; C:\Xalan\xalan-j_2_7_0\xml-apis.jar; C:\Xalan\xalan-j_2_7_0\xercesImpl.jar; C:\Xalan\xalan-j_2_7_0\xalansamples.jar; C:\Xalan\xalan-j_2_7_0\xalanservlet.jar; C:\Xalan\xalan-j_2_7_0\\JavaSDK\jdk\lib; 

  6. In a working directory (for example, C:\AAA), create insured.xml, as shown here, and make sure that the first line starts at the first position of the file:

     <?xml version="1.0" encoding=" ISO-8859-1"?> <!- CarPolicy applicant -> <Insured Customer>    <CarPolicy PolicyType=" Auto">       <Vehicle Category=" Sedan">          <Make>Honda</Make>          <Model>Accord</Model>       </Vehicle>       <Vehicle Category=" Sport" Domestic=" True">          <Make>Ford</Make>          <Model>Mustang</Model>       </Vehicle>    </CarPolicy>    <CarPolicy PolicyType=" Antique">       <Vehicle Category=" Sport">          <Make>Triumph</Make>          <Model>Spitfire</Model>       </Vehicle>       <Vehicle Category=" Coupe" Domestic=" True">          <Make>Buick</Make>          <Model>Skylark</Model>       </Vehicle>       <Vehicle Category=" Sport">          <Make>Porsche</Make>          <Model>Speedster</Model>       </Vehicle>    </CarPolicy> </Insured> 

  7. In the same working directory, create test.xsl, which is an Extensible Stylesheet Transformation (XSLT) style sheet, as shown here:

     <?xml version="1.0" encoding=" ISO-8859-1"?> <xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="1.0">   <xsl:template match="/">     <html>       <head>         <title>Testing XPath 1.0</title>       </head>       <body>         <p>         <xsl:value-of select= "/Insured/CarPolicy/Vehicle[@Category='Coupe']/Make"/>         </p>       </body>     </html>   </xsl:template> </xsl:stylesheet> 

    The XPath expression that you will change is in the middle of the file, in quotation marks. You need not change anything else in that file.

  8. To open a command window:

    1. Select Start > Run.

    2. Type cmd and press Enter.

      At the command prompt, type the following string on one line, making whatever changes are needed to correspond with your directory and file names:

       java org.apache.xalan.xslt.Process -IN C:\AAA\insured.xml -XSL C:\AAA\test.xsl -OUT C:\AAA\test.html 

    If you exclude the line that begins with -OUT, the output goes to the command window.

    Press Enter. If you included the line that begins with -OUT, inspect the last listed file by opening it in a browser. If you excluded the line with -OUT, you will view HTML tags and will need to locate the data that resides in between the paragraph (<p> </p>) tags, which are inside the body (<body> </body>) tags.

If you are using the files insured.xml and test.xsl as shown earlier, the output displays the string Buick for reasons described in Chapter 6.

Revise the XSL and XML files as you wish, testing the effect of different XPath expressions in relation to different XML source.

To rerun the command at the command prompt, click F3 or the Up Arrow and then press Enter.

If you are using a browser to review the output, you can click the browser's Refresh button to see the last result.




SOA for the Business Developer. Concepts, BPEL, and SCA
SOA for the Business Developer: Concepts, BPEL, and SCA (Business Developers series)
ISBN: 1583470654
EAN: 2147483647
Year: 2004
Pages: 157
Authors: Ben Margolis

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