INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

saxon xpath

saxon xpath

(OP)
with

com.sun.org.apache.xpath.internal.jaxp.XPathImpl

CODE

private String getProductCode() {
        String productCode = null;
        String fileName = location + "/" + originalClientFile;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory
                .newInstance();
        docFactory.setNamespaceAware(false);
        try {
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document client = docBuilder.parse(fileName);
            XPath xpath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xpath
                    .compile("/ClientRoot/Client/Policy/Identification/ProductIdentifier");
            Object result = expr.evaluate(client, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            if (nodes.getLength() > 0) {
                Node item = nodes.item(0);
                NamedNodeMap attr = item.getAttributes();
                Node mainIdentifierAttr = attr.getNamedItem("mainIdentifier");
                Node secondaryIdentifierAttr = attr
                        .getNamedItem("secondaryIdentifier");
                mainProductCode = mainIdentifierAttr.getTextContent();
                secondaryProductCode = secondaryIdentifierAttr.getTextContent();
                productCode = mainProductCode + "_" + secondaryProductCode;
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return productCode;
    }

with

net.sf.saxon.xpath.XPathEvaluator

CODE

private String getProductCode() {
        String productCode = null;
        String fileName = location + "/" + originalClientFile;

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        try {
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document client = docBuilder.parse(fileName);

            NamespaceContext ctx = new NamespaceContext() {
                public String getNamespaceURI(String prefix) {
                    String uri;
                    if (prefix.equals("ns1"))
                        uri = "http://quotescenario.framework.domain.projections.bil.ie";
                    else if (prefix.equals("ns2"))
                        uri = "http://client.framework.domain.projections.bil.ie";
                    else if (prefix.equals("ns3"))
                        uri = "http://policy.framework.domain.projections.bil.ie";
                    else if (prefix.equals("ns4"))
                        uri = "http://generaltypes.common.framework.domain.projections.bil.ie";
                    else
                        uri = null;
                    return uri;
                }

                // Dummy implementation - not used!
                public Iterator getPrefixes(String val) {
                    return null;
                }

                // Dummy implemenation - not used!
                public String getPrefix(String uri) {
                    return null;
                }
            };

            XPath xpath = XPathFactory.newInstance().newXPath();
            xpath.setNamespaceContext(ctx);
            XPathExpression expr = xpath.compile("/ns2:ClientRoot/ns2:Client/ns2:Policy/ns3:Identification/ns3:ProductIdentifier");
            Object result = expr.evaluate(client, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            if (nodes.getLength() > 0) {
                Node item = nodes.item(0);
                NamedNodeMap attr = item.getAttributes();
                Node mainIdentifierAttr = attr.getNamedItem("mainIdentifier");
                Node secondaryIdentifierAttr = attr.getNamedItem("secondaryIdentifier");
                mainProductCode = mainIdentifierAttr.getTextContent();
                secondaryProductCode = secondaryIdentifierAttr.getTextContent();
                productCode = mainProductCode + "_" + secondaryProductCode;
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return productCode;
    }


How can I get the saxon implementation to work without specifying the namespace (i.e. XPathExpression expr = xpath
                    .compile("/ClientRoot/Client/Policy/Identification/ProductIdentifier");)


Regards,

Brian  

RE: saxon xpath

(OP)
Hi Dian,

I tried the first code as is on saxon but it doesn't return any nodes. I had to do it the second way on saxon to get it to work. My question is how do I get the first code to work with saxon.


Regards,

Brian

RE: saxon xpath

(OP)
I got around my problem with the following;

CODE

XPathFactory factory = new org.apache.xpath.jaxp.XPathFactoryImpl();
            XPath xpath = factory.newXPath();

Actually, this is what i really wanted to do. The jar for saxon is included in the project, but I wanted to use this implementation.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close