xsl-list
[Top] [All Lists]

[xsl] Namespace'd XPath expression in Java

2007-04-05 05:28:47
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.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>