Vignesh Waran
Vignesh Waran

Reputation: 3

Xpath 2.0 functions not working in Java using Saxon

I'm writing a simple code to scrape data from the web page using selenium and xpath2.0 function.

Since Selenium supports only xpath1.0 functions, I am trying to use Saxon.jar

  1. I have downloaded and extracted the Saxon9he.jar files into the path "C:\Program Files\Java\jre1.8.0_111\lib\ext"
  2. I have created a file "jaxp.properties" with the following lines: javax.xml.transform.TransformerFactory = net.sf.saxon.TransformerFactoryImpl javax.xml.xpath.XPathFactory","net.sf.saxon.xpath.XPathFactoryImpl
  3. Also included my jar files in the eclipse library.

But, I am not able to fetch the values with the Xpath2.0 functions.

In my code, if I use

XPathFactory factory = XPathFactory.newInstance();

instead of

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

I am able to use the xpath1.0 functions. But I need Xpath2.0 function. please guide me in this.

My code is:

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException;
import javax.xml.xpath.XPathFunctionResolver;
import javax.xml.xpath.XPathVariableResolver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import net.sf.saxon.lib.NamespaceConstant;
import net.sf.saxon.xpath.XPathFactoryImpl;


public class XpathCheckClass {

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathFactoryConfigurationException, XPathExpressionException{


WebDriver dr = new FirefoxDriver();

dr.get("http://s15.a2zinc.net/clients/hartenergy/midstream17/Public/eBooth.aspx?Nav=False&BoothID=137384");
try {

Thread.sleep(3000);

} catch (Exception e) {

}

String source = dr.getPageSource();

Document doc = null;

try {

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

doc = db.parse( new InputSource( new StringReader(source)));

} catch (Exception e) {
e.printStackTrace();
}

System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl");
XPathFactory factory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);

//    XPathFactory factory = XPathFactory.newInstance();   ---> default xpath factory

XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("if(//h2) then //h2 else //h1");

NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

System.out.println(nodes.getLength());

for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getTextContent()); 
}


dr.close();
}

}

Upvotes: 0

Views: 1334

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

Recent releases of Saxon no longer advertise themselves as JAXP XPath services, so you need to instantiate the XPath factory explicitly:

XPathFactory xf = new net.sf.saxon.XPathFactoryImpl();

Upvotes: 1

Related Questions