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  

Perfect - works like a charm. Thanks so much Christine and Keshlam for a
simple fix to a stupid mistake!

Jason

On 5/10/07, Christine Li <jycli@(protected)> wrote:
>
>
> Hi, Jason
>
> Set namespace aware to true on DocumentBuilderFactory.
>
> DocumentBuilderFactory fct = DocumentBuilderFactory.newInstance ();
> fct.setNamespaceAware(true);
>
> Christine Li
> XSLT Development
> IBM Toronto Lab
> Tel: (905)413-2601
> Email: jycli@(protected)
>
>
>  *"Jason Morris" <jason.morris@(protected)>*
> Sent by: jm56692@(protected)
>
> 10/05/2007 12:19 PM
>   To
> xalan-j-users@(protected)  cc
>
>  Subject
> Weird behavior of XPath.evaluate()
>
>
>
>
>
>
> 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
> * <http://xml.apache.org/xalan-j/xpath_apis.html#namespacecontext>
>
> <?xml version='1.0'?>
>
> <foo:document xmlns:foo="*http://apache.org/foo* <http://apache.org/foo>"
> xmlns:bar="* http://apache.org/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* <http://apache.org/foo>";
>        else if ( prefix.equals("bar"))
>            return "*http://apache.org/bar* <http://apache.org/bar>";
>        else
>            return XMLConstants.NULL_NS_URI;
>    }
>
>    public String getPrefix(String namespace)
>    {
>        if (namespace.equals("*http://apache.org/foo*<http://apache.org/foo>
> "))
>            return "foo";
>        else if (namespace.equals("* http://apache.org/bar*<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
>
>

Perfect - works like a charm. Thanks so much Christine and Keshlam for a simple
fix to a stupid mistake!<br><br>Jason<br><br><div><span class="gmail_quote">On
5/10/07, <b class="gmail_sendername">Christine Li</b> &lt;<a href="mailto:jycli
@(protected)">
jycli@(protected)</a>&gt; wrote:</span><blockquote class="gmail_quote" style=
"border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding
-left: 1ex;">
<br><font face="sans-serif" size="2">Hi, Jason</font>
<br>
<br><font face="sans-serif" size="2">Set namespace aware to true on
DocumentBuilderFactory.</font>
<br>
<br><font size="3">DocumentBuilderFactory fct = DocumentBuilderFactory
.newInstance
();</font>
<br><font face="sans-serif" size="2">fct.setNamespaceAware(true);</font>
<br>
<br><font face="sans-serif" size="2">Christine Li<br>
XSLT Development<br>
IBM Toronto Lab<br>
Tel: (905)413-2601<br>
Email: <a href="mailto:jycli@(protected)" target="_blank" onclick="return top.js
.OpenExtLink(window,event,this)">jycli@(protected)</a></font>
<br>
<br>
<br>
<table width="100%">
<tbody><tr valign="top">
<td width="40%"><font face="sans-serif" size="1"><b>&quot;Jason Morris&quot;
&lt;<a href="mailto:jason.morris@(protected)" target="_blank" onclick="return
top.js.OpenExtLink(window,event,this)">jason.morris@(protected)</a>&gt;</b> <
/font>
<br><font face="sans-serif" size="1">Sent by: <a href="mailto:jm56692@(protected)
" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">jm56692
@(protected)</a></font>
<p><font face="sans-serif" size="1">10/05/2007 12:19 PM</font>
</p></td><td width="59%">
<table width="100%">
<tbody><tr valign="top">
<td>
<div align="right"><font face="sans-serif" size="1">To</font></div>
</td><td><font face="sans-serif" size="1"><a href="mailto:xalan-j-users@(protected)
.apache.org" target="_blank" onclick="return top.js.OpenExtLink(window,event
,this)">xalan-j-users@(protected)</a></font>
</td></tr><tr valign="top">
<td>
<div align="right"><font face="sans-serif" size="1">cc</font></div>
</td><td>
<br></td></tr><tr valign="top">
<td>
<div align="right"><font face="sans-serif" size="1">Subject</font></div>
</td><td><font face="sans-serif" size="1">Weird behavior of XPath.evaluate()<
/font></td></tr></tbody></table>
<br>
<table>
<tbody><tr valign="top">
<td>
<br></td><td><br></td></tr></tbody></table>
<br></td></tr></tbody></table><div><span class="e" id="q_11277299af316289_1">
<br>
<br>
<br><font size="3">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 </font><a href="http://xml.apache.org/xalan-j/xpath
_apis.html#namespacecontext" target="_blank" onclick="return top.js.OpenExtLink
(window,event,this)"><font color="blue" size="3"><u>http://xml.apache.org/xalan
-j/xpath_apis.html#namespacecontext
</u></font></a><font size="3"><br>
<br>
&lt;?xml version=&#39;1.0&#39;?&gt;<br>
<br>
&lt;foo:document xmlns:foo=&quot;</font><a href="http://apache.org/foo" target
="_blank" onclick="return top.js.OpenExtLink(window,event,this)"><font color=
"blue" size="3"><u>http://apache.org/foo</u></font></a><font size="3">
&quot;
xmlns:bar=&quot;</font><a href="http://apache.org/bar" target="_blank" onclick=
"return top.js.OpenExtLink(window,event,this)"><font color="blue" size="3"><u>
http://apache.org/bar</u></font></a><font size="3">&quot;&gt;<br>
&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;public static void main(String[] argv) throws Exception<br>
&nbsp; &nbsp;{<br>
&nbsp; &nbsp; &nbsp; &nbsp;String doc = &quot; namespace.xml&quot;;<br>
&nbsp; &nbsp; &nbsp; &nbsp;String xPath = &quot;/foo:document/bar:element&quot
;;<br>
<br>
&nbsp; &nbsp; &nbsp; &nbsp;// Parse the doc independently to get the root
Element<br>
&nbsp; &nbsp; &nbsp; &nbsp;DocumentBuilderFactory fct = DocumentBuilderFactory
.newInstance
();<br>
&nbsp; &nbsp; &nbsp; &nbsp;DocumentBuilder builder = fct.newDocumentBuilder();
<br>
&nbsp; &nbsp; &nbsp; &nbsp;Document document = builder.parse(&quot;file:&quot;
+ doc);<br>
&nbsp; &nbsp; &nbsp; &nbsp;Element rootElement = document.getDocumentElement()
;<br>
<br>
&nbsp; &nbsp; &nbsp; &nbsp;// Setup the namespace context <br>
&nbsp; &nbsp; &nbsp; &nbsp;XPathFactory factory = XPathFactory.newInstance();
<br>
&nbsp; &nbsp; &nbsp; &nbsp;XPath xp = factory.newXPath();<br>
&nbsp; &nbsp; &nbsp; &nbsp;NamespaceContext ctx = new MyNamespaceContext();<br>
&nbsp; &nbsp; &nbsp; &nbsp;xp.setNamespaceContext(ctx);<br>
&nbsp; &nbsp; &nbsp; &nbsp;<br>
&nbsp; &nbsp; &nbsp; &nbsp;// Create an InputSource of the file <br>
&nbsp; &nbsp; &nbsp; &nbsp;InputSource is = new InputSource(doc);<br>
&nbsp; &nbsp; &nbsp; &nbsp;<br>
&nbsp; &nbsp; &nbsp; &nbsp;// This works<br>
&nbsp; &nbsp; &nbsp; &nbsp;String resultString1 = xp.evaluate(xPath,is);<br>
&nbsp; &nbsp; &nbsp; &nbsp;// This doesn&#39;t<br>
&nbsp; &nbsp; &nbsp; &nbsp;String resultString2 = xp.evaluate (xPath
,rootElement);<br>
<br>
&nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;RES1=&quot; +
resultString1);<br>
&nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;RES2=&quot; +
resultString2);<br>
&nbsp; &nbsp;}<br>
}<br>
<br>
<br>
<br>
class MyNamespaceContext implements NamespaceContext <br>
{<br>
&nbsp; &nbsp;public String getNamespaceURI(String prefix)<br>
&nbsp; &nbsp;{<br>
&nbsp; &nbsp; &nbsp; &nbsp;if (prefix.equals(&quot;foo&quot;))<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &quot;</font><a href="http:/
/apache.org/foo" target="_blank" onclick="return top.js.OpenExtLink(window,event
,this)"><font color="blue" size="3"><u>
http://apache.org/foo</u></font></a><font size="3">&quot;;<br>
&nbsp; &nbsp; &nbsp; &nbsp;else if ( prefix.equals(&quot;bar&quot;))<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &quot;</font><a href="http:/
/apache.org/bar" target="_blank" onclick="return top.js.OpenExtLink(window,event
,this)"><font color="blue" size="3"><u>http://apache.org/bar</u></font></a><font
size="3">&quot;;
<br>
&nbsp; &nbsp; &nbsp; &nbsp;else<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return XMLConstants.NULL_NS_URI;<br>
&nbsp; &nbsp;}<br>
&nbsp; &nbsp;<br>
&nbsp; &nbsp;public String getPrefix(String namespace) <br>
&nbsp; &nbsp;{<br>
&nbsp; &nbsp; &nbsp; &nbsp;if (namespace.equals(&quot;</font><a href="http:/
/apache.org/foo" target="_blank" onclick="return top.js.OpenExtLink(window,event
,this)"><font color="blue" size="3"><u>http://apache.org/foo</u></font></a><font
size="3">
&quot;))<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &quot;foo&quot;; <br>
&nbsp; &nbsp; &nbsp; &nbsp;else if (namespace.equals(&quot;</font><a href=
"http://apache.org/bar" target="_blank" onclick="return top.js.OpenExtLink
(window,event,this)"><font color="blue" size="3"><u>
http://apache.org/bar</u></font></a><font size="3">&quot;))<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &quot;bar&quot;;<br>
&nbsp; &nbsp; &nbsp; &nbsp;else<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return null;<br>
&nbsp; &nbsp;}<br>
<br>
&nbsp; &nbsp;public Iterator getPrefixes(String namespace)<br>
&nbsp; &nbsp;{<br>
&nbsp; &nbsp; &nbsp; &nbsp;return null;<br>
&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>
</font>
<br></span></div></blockquote></div><br>