Thanks for the quick response Michael; I will give the s9api a go.

 

Out of interest through;

 

Lets say in my case both factories where initialised using a different Configuration/NamePools. Why does it work in the second case (TEST_02.xml) and not in the first case? Is it pot luck?

 

In my original email, I gave a trimmed down version of the XML document (to make things easier) and the ‘problematic’ XPath mentioned was the only one to cause an issue. If I move the <swPrivateData> node to another point in the document, the XPath works fine.

 

Cheers,

NP16

 


From: Michael Kay [XXX]
Sent: 10 January 2011 17:02
Subject: Re: [saxon] Saxon v9.1.0.8 - XPaths problems

 

This is failing because the DocumentBuilderFactory and the XPathFactory use different Saxon Configurations and hence different NamePools; so the internal codes allocated to the names in the source tree don't match those used in the compiled XPath expression.

More recent releases than 9.1.0.8 detect this problem and throw an exception when you try to do this.

It's in any case a very inefficient way of using the Saxon XPath processor. The JAXP DocumentBuilder always builds a DOM, and the Saxon implementation (which is now deprecated) builds this as a wrapper over Saxon's internal tree structure. This wrapper layer only slows the XPath engine down.

I'd strongly recommend using the s9api interface for an application like this, rather than using JAXP. There are many disadvantages in the JAXP interface - its bias towards the DOM, its lack of XPath 2.0 support, its weak typing, its clumsy support for external functions and variables. The JAXP XPath interface supports multiple tree models (at least in theory), but the DocumentBuilder interface does not. This means that there's unfortunately no way of constructing a tree and then running multiple XPath expressions on it with Saxon using pure JAXP interfaces; you have to expose Saxon somewhere (for example by ensuring that the DocumentBuilderFactory and the XPathFactory use the same underlying Configuration); and if you're going to do that, you might as well use Saxon interfaces throughout.

If you do want to use the JAXP XPathFactory, however, you can get its Configuration by casting to the Saxon implementation class, and then you can use configuration.buildDocument to create a source document in Saxon's native tree format.

Michael Kay
Saxonica

On 10/01/2011 16:10, Niraj.Patel@barclayscapital.com wrote:

All,

 

I am having problems using Saxon 9.1.0.8 to evaluate an XPath. If I remove an element from the XML, the XPath evaluates ok.

 

From the contents below, TEST_01.xml contains the extra element and TEST_02.xml does not. When the XPath is applied to TEST_01.xml nothing is returned, but the same XPath applied to TEST_02.xml returns a value.

 

Could you please help? Am I initialising the DocumentBuilder and XPathFactory correctly?

 

Cheers,

NP16

 

*** Classpath:

 

Java 1.6

saxon-9.1.0.8.jar

saxon-dom-9.1.0.8.jar

saxon-sql-9.1.0.8.jar

saxon-xpath-9.1.0.8.jar

 

*** Java Test:

 

import java.io.File;

import java.io.FileInputStream;

 

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.xpath.XPath;

import javax.xml.xpath.XPathFactory;

 

import net.sf.saxon.om.NamespaceConstant;

 

import org.w3c.dom.Document;

import org.xml.sax.InputSource;

 

public class Test

{

    public static void main(String[] args) throws Exception

    {

        // Set props

        System.setProperty(

            "javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON,

            "net.sf.saxon.xpath.XPathFactoryImpl"

        );

        System.err.println("Java version  : " + System.getProperty("java.runtime.version"));

        System.err.println("");

 

        test("TEST_01.xml");

        test("TEST_02.xml");

    }

 

    private static void test(String fileName) throws Exception

    {

        // Load XML document

        System.err.println("Loading XML file: " + fileName);

 

        DocumentBuilderFactory builder = new net.sf.saxon.dom.DocumentBuilderFactoryImpl();

        File file = new File(

            Test.class.getClassLoader().getResource(fileName).getPath()

        );

        Document doc = builder.newDocumentBuilder().parse(new InputSource(new FileInputStream(file)));

 

        System.err.println("XML Document instance: " + doc.getClass().getName());

 

        // Initialise and exec xpath

        String query = "/xxx/swPrivateData/foo";

        System.err.println("Xpath: " + query);

 

        XPathFactory xpf = new net.sf.saxon.xpath.XPathFactoryImpl();

        XPath xp = xpf.newXPath();

 

        System.err.println("XPath instance: " + xp.getClass().getName());

        System.err.println("XPath results: '" + xp.evaluate(query, doc) + "'");

        System.err.println("");

    }

}

 

*** TEST_01.xml

 

<xxx>

  <equityExpirationTimeType>FOOBAR</equityExpirationTimeType>

  <swPrivateData>

    <foo>1</foo>

  </swPrivateData>

</xxx>

 

*** TEST_02.xml

 

<xxx>

  <swPrivateData>

    <foo>1</foo>

  </swPrivateData>

</xxx>

 

*** Output:

 

Java version  : 1.6.0_20-b02

 

Loading XML file: TEST_01.xml

XML Document instance: net.sf.saxon.dom.DocumentOverNodeInfo

Xpath: /xxx/swPrivateData/foo

XPath instance: net.sf.saxon.xpath.XPathEvaluator

XPath results: ''

 

Loading XML file: TEST_02.xml

XML Document instance: net.sf.saxon.dom.DocumentOverNodeInfo

Xpath: /xxx/swPrivateData/foo

XPath instance: net.sf.saxon.xpath.XPathEvaluator

XPath results: '1'

_______________________________________________

 

This e-mail may contain information that is confidential, privileged or otherwise protected from disclosure. If you are not an intended recipient of this e-mail, do not duplicate or redistribute it by any means. Please delete it and any attachments and notify the sender that you have received it in error. Unless specifically indicated, this e-mail is not an offer to buy or sell or a solicitation to buy or sell any securities, investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Barclays. Any views or opinions presented are solely those of the author and do not necessarily represent those of Barclays. This e-mail is subject to terms available at the following link: www.barcap.com/emaildisclaimer. By messaging with Barclays you consent to the foregoing.  Barclays Capital is the investment banking division of Barclays Bank PLC, a company registered in England (number 1026167) with its registered office at 1 Churchill Place, London, E14 5HP.  This email may relate to or be sent from other members of the Barclays Group.

_______________________________________________