Subjects
Home
Xalan extension functions
Fomatting question serializing DOM with pretty print
xalan with pull parser
Cannot find the declaration
Apache Xalan drop support to run on JRE 1 1 x
Why does Doctype change processing of a document
Node set to XML string via Java extensions in Xalan J: possible?
Templates/Transformers + thread safety???
Problem evaluating xpath with muliple prefix with different namespace
remove an arbitrary attribute from xsl output
Xalan3 XSLT 2 0 XPath 2 0 support?
Problem using compiled translets with Xalan !!
Xalan and jstl 1 1 problem with transform tag
NullPointer in DOM2DTM getLocalName
URIResolvers base parameter with xsltc and cascaded imports
Performance problem for Xalan J on intel dual core
Standard libraries in JAXP?
Serializing a DOM tree to XML file, customize entities replacement
Library Conflict Involving BCEL Library
A question on how users are using <xsl:message >
Kevin Cormier as a new Apache Xalan J committer
Struggling to iterate over tokenized string
Xalan count() trouble
Problem with recursive xpath
Error when switching to java 1 5
document( ' ')
Problem with Xalan2 7 0 transformation
cr/lf options
entity encoded XML
can xalan transform 2 xml using one xslt?
Xalan J JIRA defect review Monday October 16, 2006 from 2:00 to 3:30 pm ED
xsl transform with cdata section elements
xslt parameters not expanded
Weird behavior of XPath evaluate()
How to avoid <xsl:message > instruction prints stylesheet file informations ?
Cannot find SimpleTransform subdirectory after installing Xalan J
recover from document not found exceptions
jdk1 5 and Xalan jar differences?
Performance Issue
Error/Bug adding floating point numbers
XPathAPI: eval exp using nodes with default namespace
modifying xalan to output invalid XML
NullPointerException
mege two separate xml nodes into one
Is this a XALAN document identification bug?
is StylesheetRoot really java io Serializable ?
transform() fails for DOMSource but succeeds for StreamSource
Thoughts on Transformer parameter passing
HELP, Xalan and jstl 1 1 problem with transformer
Problem with XPath namespace axis?
string utils:replace deleting search string if replacement string is an HTML
help with enumeration values pls
xalan 2 5 1 vs 2 7 performance question
How to insert/update in XML document
HTML Serialization and Handling of Ampersands in HREF Attributes
XHTML link tag stripping
SystemId Unknown; Line #24; Column #49; java lang NullPointerException
xpath text() help
Apostrophe problem with xalan 2 7 0
How to set variables in XML document?
Links
Home
Oracle database error code ...
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
Weird behavior of XPath.evaluate()

Weird behavior of XPath.evaluate()

2007-05-10       - By Jason Morris
Reply:     1     2     3     4  

Hi all,

Apologies if this has been asked before but it is driving me mad. I am
happily able to evaluate non-namespaced XPath expressions using either an
InputSource or a document context element. I can evaluate namespaced
expressions using the InputSource, but it doesn't work if I evaluate a
namespaced expression passing an Element as a context node.

Take the example file from
http://xml.apache.org/xalan-j/xpath_apis.html#namespacecontext

<?xml version='1.0'?>

<foo:document xmlns:foo="http://apache.org/foo" xmlns:bar="
http://apache.org/bar">
   <bar:element>MyBar</bar:element>
 </foo:document>

I want to evaluate "/foo:document/bar:element", which should return "MyBar".
I use the following code:

package test;

import java.util.Iterator;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory ;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

public class NamespaceTest
{
   public static void main(String[] argv) throws Exception
   {
       String doc = " namespace.xml";
       String xPath = "/foo:document/bar:element";

       // Parse the doc independently to get the root Element
       DocumentBuilderFactory fct = DocumentBuilderFactory.newInstance ();
       DocumentBuilder builder = fct.newDocumentBuilder();
       Document document = builder.parse("file:" + doc);
       Element rootElement = document.getDocumentElement();

       // Setup the namespace context
       XPathFactory factory = XPathFactory.newInstance();
       XPath xp = factory.newXPath();
       NamespaceContext ctx = new MyNamespaceContext();
       xp.setNamespaceContext(ctx);

       // Create an InputSource of the file
       InputSource is = new InputSource(doc);

       // This works
       String resultString1 = xp.evaluate(xPath,is);
       // This doesn't
       String resultString2 = xp.evaluate (xPath,rootElement);

       System.out.println("RES1=" + resultString1);
       System.out.println("RES2=" + resultString2);
   }
}



class MyNamespaceContext implements NamespaceContext
{
   public String getNamespaceURI(String prefix)
   {
       if (prefix.equals("foo"))
           return "http://apache.org/foo";
       else if ( prefix.equals("bar"))
           return "http://apache.org/bar";
       else
           return XMLConstants.NULL_NS_URI;
   }

   public String getPrefix(String namespace)
   {
       if (namespace.equals("http://apache.org/foo"))
           return "foo";
       else if (namespace.equals(" http://apache.org/bar"))
           return "bar";
       else
           return null;
   }

   public Iterator getPrefixes(String namespace)
   {
       return null;
   }
}


When I execute this (against Xalan 2.7.0), resultString1 returns "MyBar"
correctly, resultString2 is set as empty string. Surely they should both
return the correct result? Interestingly, if I use the exact same code to
evaluate a namespace-free expression, both versions work fine.

Is this a bug, or am I doing something stupid with the rootElement I am
passing in?

Any and all assistance greatly appreciated,

Jason

Hi all,<br><br>Apologies if this has been asked before but it is driving me mad
. I am happily able to evaluate non-namespaced XPath expressions using either an
InputSource or a document context element. I can evaluate namespaced
expressions using the InputSource, but it doesn&#39;t work if I evaluate a
namespaced expression passing an Element as a context node.
<br><br>Take the example file from <a href="http://xml.apache.org/xalan-j/xpath
_apis.html#namespacecontext" target="_blank" onclick="return top.js.OpenExtLink
(window,event,this)">http://xml.apache.org/xalan-j/xpath_apis.html
#namespacecontext
</a><br><br>&lt;?xml version=&#39;1.0&#39;?&gt;<br>
<br>&nbsp;&lt;foo:document xmlns:foo=&quot;<a href="http://apache.org/foo"
target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http:/
/apache.org/foo</a>&quot; xmlns:bar=&quot;<a href="http://apache.org/bar" target
="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://apache.org/bar</a>&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &lt;bar:element&gt
;MyBar&lt;/bar:element&gt;
<br>&nbsp; &lt;/foo:document&gt;<br>&nbsp; <br>I want to evaluate &quot;/foo
:document/bar:element&quot;, which should return &quot;MyBar&quot;. I use the
following code:<br><br>package test;<br><br>import java.util.Iterator;<br><br>

import javax.xml.XMLConstants;<br>import javax.xml.namespace.NamespaceContext;
<br>import javax.xml.parsers.DocumentBuilder;<br>import javax.xml.parsers
.DocumentBuilderFactory;<br>import javax.xml.xpath.XPath;<br>import javax.xml
.xpath.XPathFactory

;<br><br>import org.w3c.dom.Document;<br>import org.w3c.dom.Element;<br>import
org.xml.sax.InputSource;<br><br>public class NamespaceTest<br>{<br>&nbsp;&nbsp;
&nbsp; public static void main(String[] argv) throws Exception<br>&nbsp;&nbsp;
&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String doc = &quot;
namespace.xml&quot;;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String xPath =
&quot;/foo:document/bar:element&quot;;<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp; // Parse the doc independently to get the root Element<br>&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp;&nbsp; DocumentBuilderFactory fct = DocumentBuilderFactory
.newInstance

();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DocumentBuilder builder = fct
.newDocumentBuilder();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Document
document = builder.parse(&quot;file:&quot; + doc);<br>&nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; Element rootElement = document.getDocumentElement();<br><br>&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Setup the namespace context
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; XPathFactory factory = XPathFactory
.newInstance();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; XPath xp = factory
.newXPath();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; NamespaceContext ctx = new
MyNamespaceContext();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; xp
.setNamespaceContext(ctx);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Create an InputSource of the file
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; InputSource is = new InputSource(doc)
;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp; // This works<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String
resultString1 = xp.evaluate(xPath,is);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
// This doesn&#39;t<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String
resultString2 = xp.evaluate

(xPath,rootElement);<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out
.println(&quot;RES1=&quot; + resultString1);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp; System.out.println(&quot;RES2=&quot; + resultString2);<br>&nbsp;&nbsp;
&nbsp; }<br>}<br><br><br><br>class MyNamespaceContext implements
NamespaceContext
<br>{<br>&nbsp;&nbsp;&nbsp; public String getNamespaceURI(String prefix)<br>
&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (prefix
.equals(&quot;foo&quot;))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; return &quot;<a href="http://apache.org/foo" target="_blank"
onclick="return top.js.OpenExtLink(window,event,this)">
http://apache.org/foo</a>&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
else if (
prefix.equals(&quot;bar&quot;))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; return &quot;<a href="http://apache.org/bar" target="
_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://apache.org
/bar</a>&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return
XMLConstants.NULL_NS_URI;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; public String getPrefix(String namespace)
<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
(namespace.equals(&quot;<a href="http://apache.org/foo" target="_blank" onclick=
"return top.js.OpenExtLink(window,event,this)">http://apache.org/foo</a>&quot;))
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return
&quot;foo&quot;;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (namespace.equals(&quot;
<a href="http://apache.org/bar" target="_blank" onclick="return top.js
.OpenExtLink(window,event,this)">
http://apache.org/bar</a>&quot;))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; return &quot;bar&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp; return null;<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;
public Iterator getPrefixes(String namespace)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<br>&nbsp;&nbsp;&nbsp; }
<br>}&nbsp; <br><br><br>When I execute this (against Xalan 2.7.0),
resultString1 returns &quot;MyBar&quot; correctly, resultString2 is set as
empty string. Surely they should both return the correct result? Interestingly,
if I use the exact same code to evaluate a namespace-free expression, both
versions work fine.
<br><br>Is this a bug, or am I doing something stupid with the rootElement I am
passing in?<br><br>Any and all assistance greatly appreciated,<br><br>Jason<br>
<br>