xml editor

Supported platforms

Compatible with Windows7 & Mac OS X Snow Leopard

Ready for data server software
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Namespace'd XPath expression in Java


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

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
Keywords
XML Editor | XML Author | WYSIWYG Editors | Schema Editor | XSD Documentation | XSL/XSLT Editor | XQuery | XML Databases | SVN Client
© 2002-2011 SyncRO Soft Ltd. All rights reserved. | Sitemap | Privacy Policy | This website was created & generated with <oXygen/>®XML Editor