Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saxon xpath

Status
Not open for further replies.

keenanbr

Programmer
Oct 3, 2001
469
IE
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 = "[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
 
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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top