[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
[Recent Entries]
[Reply To This Message]
Re: Namespace'd XPath expression in Java
Subject: Re: Namespace'd XPath expression in Java
From: "Jacek Radajewski" <jacekrad@xxxxxxxxx>
Date: Fri, 6 Apr 2007 09:09:36 +1000
|
See below.
J.
Here is my Java code:
______________________________
// load XSD
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); <----------------------
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document xsdDoc = docBuilder.parse("path to my xsd");
// eval XPath
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new MyNamespaceContext());
System.out.println(xpath.evaluate("count(//xs:element)",
xsdDoc.getDocumentElement()));
______________________________
And finally implementation of NamespaceContext:
______________________________
public class MyNamespaceContext implements NamespaceContext {
public String getNamespaceURI(String prefix) {
if ("xs".equals(prefix)) {
return "http://www.w3.org/2001/XMLSchema";
} else if ("msdata".equals(prefix)) {
return "urn:schemas-microsoft-com:xml-msdata";
}
return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String namespaceURI) {
if ("http://www.w3.org/2001/XMLSchema".equals(namespaceURI)) {
return "xs";
} else if ("urn:schemas-microsoft-com:xml-msdata".equals(namespaceURI)) {
return "msdata";
}
return XMLConstants.DEFAULT_NS_PREFIX;
}
public Iterator<String> getPrefixes(String namespaceURI) {
List<String> uris = new ArrayList<String>();
uris.add(getPrefix(namespaceURI));
return uris.iterator();
}
}
______________________________
Thanks in advance.
--
Jacek Radajewski
|
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format
RSS 2.0 |
|
Atom 0.3 |
|
|