XPath in Java, .NET, and PHP


You can evaluate an XPath expression from a tool, like an XML editor. You will learn about some of the tools that can be used for this purpose in the section "Tools for XPath". But you often need to evaluate an XPath expression from one of your programs, which in most cases means that you will need to use an XPath library. In what follows, you will learn how to use XPath libraries to evaluate XPath expressions in three programming languages: Java, C#, and PHP.

XPath in Java

The Java API for XML Processing (JAXP) is a standard Java API that includes an API for XPath. So using JAXP, you can write code in a way that is mostly independent of a particular XPath implementation. If you write some code using JAXP running a current implementation of XPath, you will be able to easily switch to another XPath implementation in the future. JAXP not only makes your code portable to other XPath implementations, but it also makes your knowledge portable. You will be able to use what you learn here with any implementation of XPath.

JAXP comes out-of-the-box with J2SE 5 and newer. If you are using J2SE 4, you can download the Sun JAXP package from https://www.jaxp.dev.java.net/. Follow this step-by-step procedure to learn how to use JAXP from your Java program:

Note 

The code in this procedure uses the Saxon implementation of the JAXP. There are two versions of Saxon: Saxon-SA is the schema aware version, and Saxon-B is the open source version. The only limitation of Saxon-B is that it does not provide any of the schema-aware features, which is not a concern for what you are doing here. You can find more about Saxon and download it from http://www.saxon.sourceforge.net/.

  1. All the classes used in the following steps are declared here. Some come from the JAXP library, others come from Saxon, and one comes from SAX (the Simple API for XML).

          // Classes from JAXP      import javax.xml.xpath.XPathFactory;      import javax.xml.xpath.XPath      import javax.xml.xpath.XpathExpression;      import javax.xml.transform.sax.SAXSource;      // Class from SAX      import org.xml.sax.InputSource      // Classes from Saxon      import net.sf.saxon.xpath.XPathEvaluator;      import net.sf.saxon.om.NamespaceConstant; 
  2. Create an XPath factory. To do this, call the JAXP method XPathFactory.newInstance() and pass a URI as parameter. The URI you need to pass to use the Saxon implementation is http://www.saxon.sf.net/jaxp/xpath/om. Instead of using the literal string, you reference the public static final string NamespaceConstant.OBJECT_MODEL_SAXON as follows:

          XPathFactory xpathFactory =              XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); 

  3. Use the factory to create XPath object. The object returned by the factory implements the interface javax.xml.xpath.XPath, but because this code is using the Saxon implementation, you know that it is an instance of net.sf.saxon.xpath.XPathEvaluator so you can just do this:

          XPath xpath = xpathFactory.newXPath(); 
  4. The object that represents the XML document on which the XPath expression is evaluated is XPath implementation dependent. With Saxon, this object implements the net.sf.saxon.om.NodeInfo interface, and you create it with the instance of XPathEvaluator from step 3. The XPathEvalutor.setSource() method used on the third line takes a SAXSource, which is created based on the URL of the XML file on which the XPath expression will be evaluated:

          InputSource inputSource = new InputSource("http://www.example.com/catalog.xml");      SAXSource saxSource = new SAXSource(inputSource);      NodeInfo nodeInfo = ((XPathEvaluator) xpath).setSource(saxSource); 

  5. Now that you have all those objects in place, you can directly evaluate an XPath expression with xpath.evalute(). Instead, the following code takes the long route-it first compiles the expression, and then evaluates the expression: You will want to compile your expressions if you are evaluating them multiple times, in which case your program will run more efficiently because the XPath expression will be analyzed and compiled only once.

          XPathExpression expression = xpath.compile("local-name(/*)"); 

  6. Evaluate the expression you just compiled, passing the object that represents the XML document you created in step 4. Specify the type of object you expect in return with the second argument. In this case, you specify the String type, so you can safely cast the object returned by evaluate() to String:

          expression.evaluate(nodeInfo, XPathConstants.STRING); 
Note 

The full source code for this program is available at http://www.wrox.com.

XPath on .NET

The XPath implementation that was used in the Java code in the previous section is Saxon. It is also available for the .NET platform. You can download Saxon for .NET from http://www.saxon.sourceforge.net/. To run an XPath expression on a document with Saxon for .NET, follow these steps:

  1. You are using System and Saxon.Api in this program:

          using System;      using Saxon.Api; 
  2. Create an instance of Processor, and then use it to create XPathCompiler and DocumentBuilder, as follows:

          Processor processor = new Processor();      XPathCompiler compiler = processor.NewXPathCompiler();      DocumentBuilder builder = processor.NewDocumentBuilder(); 

  3. The object document that represents the XML document is created by loading the file from a URL. You need to set a base URI on the builder, so it can resolve any URI that could appear in the document, such as a reference to DTDs. Here's how to do this:

          builder.BaseUri = new Uri("http://www.example.com/");      XdmNode document = builder.Build          (new Uri("http://www.example.com/catalog.xml")); 
  4. You get to an object that you can use to evaluate the XPath expression by first calling the Compile() and then Load() methods, which gives you an instance of XPathSelector:

          XPathSelector selector = compiler.Compile("local-name(/*)").Load(); 

  5. Use the instance of XPathSelector you created in step 4 to set the document that contains the expression to be evaluated, and then call the Evaluate() method to evaluate the XPath expression on the document:

          selector.ContextItem = document;      XdmValue value = selector.Evaluate();      Console.WriteLine(value.ToString()); 
Note 

The full source code for this program is available at http://www.wrox.com.

XPath in PHP

PHP ships with a set of functions that use of the GNOME XML library. Simplicity is one reason why PHP became so popular. Evaluating an XPath expression on a document is just as simple. In just four lines, you can read an XML file, evaluate an XPath expression on that document, and print the result. Consider the following program:

      $doc = domxml_open_file("catalog.xml");      $xpathContext = $doc->xpath_new_context();      $rootName = $xpathContext->xpath_eval("local-name(/*)");      print($rootName->value); 

This is what's happening here:

  • q The first line reads the XML document catalog.xml from a file on disk.

  • q The second line creates an XPath evaluation context for that document.

  • q With the XPath context, the third line evaluates the expression.

  • q The fourth line prints the result.

That's it. (You can view this as PHP's contribution to saving trees.)




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

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