[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Namespace'd XPath expression in Java

Subject: Namespace'd XPath expression in Java
From: "Mohsen Saboorian" <mohsens@xxxxxxxxx>
Date: Thu, 5 Apr 2007 15:58:24 +0330
 Namespace'd XPath expression in Java
Hi,

I have the following peace of code which works fine in IE 6, but
returns nothing with Java 5.

XPath expression:

<xsl:value-of select="count(//xs:element)" />

My XML (actually an XSD):
______________________________
<?xml-stylesheet type="text/xsl" href="rpt-xsd-transformer.xsl"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
	<xs:element name="nnn" msdata:IsDataSet="true">
		<xs:complexType>
			<xs:choice maxOccurs="unbounded">
				<xs:element name="testElement">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="name" type="xs:string" />
							<xs:element name="group" type="xs:string" minOccurs="0" />
							<xs:element name="faculty" type="xs:string" minOccurs="0" />

.....
______________________________

Here is my Java code:
______________________________
// load XSD
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
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.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.