with
com.sun.org.apache.xpath.internal.jaxp.XPathImpl
with
net.sf.saxon.xpath.XPathEvaluator
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
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 = "[URL unfurl="true"]http://quotescenario.framework.domain.projections.bil.ie";[/URL]
else if (prefix.equals("ns2"))
uri = "[URL unfurl="true"]http://client.framework.domain.projections.bil.ie";[/URL]
else if (prefix.equals("ns3"))
uri = "[URL unfurl="true"]http://policy.framework.domain.projections.bil.ie";[/URL]
else if (prefix.equals("ns4"))
uri = "[URL unfurl="true"]http://generaltypes.common.framework.domain.projections.bil.ie";[/URL]
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