tag:blogger.com,1999:blog-2083400580050165811.post5033991739319064092..comments2023-06-02T15:10:12.074+01:00Comments on Illegal Argument Exception: Java: using XPath with namespaces and implementing NamespaceContextUnknownnoreply@blogger.comBlogger11125tag:blogger.com,1999:blog-2083400580050165811.post-11254698663757487802016-09-14T18:53:08.684+01:002016-09-14T18:53:08.684+01:00I find this article http://www.ibm.com/developerwo...I find this article http://www.ibm.com/developerworks/xml/library/x-nmspccontext/index.html?ca=drs- from IBM very helpful. It provide 3 approaches:<br />1) hard coded solution. Implement the NamespaceContext interface, and hard code the mapping in the code. Only works for the xml you are targeting.<br />2) read namespaces from the document. use Document.lookupNamespaceURI(String prefix) and pengshuaihttps://www.blogger.com/profile/00747775883582581353noreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-88699474583549782582013-03-15T19:31:21.536+00:002013-03-15T19:31:21.536+00:00Thanks! I fixed it up.
(defn namespace-map
&quo...Thanks! I fixed it up.<br /><br />(defn namespace-map<br /> "Returns an implementation of NamespaceContext ... actual usefulness TBD"<br /> [mapping]<br /> (let [defaults {XMLConstants/XML_NS_PREFIX XMLConstants/XML_NS_URI<br /> XMLConstants/XMLNS_ATTRIBUTE XMLConstants/XMLNS_ATTRIBUTE_NS_URI}<br /> mapping (merge mapping defaults)<br /> prefixes (fn [Jhttps://www.blogger.com/profile/13424780351610630615noreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-34027672461116833472013-02-26T09:42:08.898+00:002013-02-26T09:42:08.898+00:00Nice, but note that your type does not meet the cl...Nice, but note that your type does not meet the class contract for <a href="http://docs.oracle.com/javase/7/docs/api/javax/xml/namespace/NamespaceContext.html" rel="nofollow">NamespaceContext</a> as it does not perform the special constant handling required by the API documentation.McDowellhttps://www.blogger.com/profile/15240682237791734569noreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-5305217402730140662013-02-25T21:21:20.832+00:002013-02-25T21:21:20.832+00:00dang, I just implemented this in Clojure as a func...dang, I just implemented this in Clojure as a function that takes a hash-map of prefixes to URI strings, and returns a full implementation of NamespaceContext, and it's literally 7 lines of code.<br /><br /> (defn namespace-map<br /> [mapping]<br /> (let [prefixes (fn [uri] (map key (filter #(= uri (val %)) mapping)))]<br /> (proxy [Object NamespaceContext] []<br />Jhttps://www.blogger.com/profile/13424780351610630615noreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-45049048312815188342011-07-10T13:55:49.374+01:002011-07-10T13:55:49.374+01:00@Anonymous - thanks for the link to the applet; I ...@Anonymous - thanks for the link to the applet; I was not aware of it.<br /><br />Prompted by the comments, I've checked <a href="http://www.w3.org/TR/REC-xml-names/#NT-PrefixedName" rel="nofollow">the spec</a>. Namespace prefixes must be at least one character long.<br /><br />Here are the relevant parts of the lexical structure:<br /><br /><b>PrefixedName ::= Prefix ':' LocalPart<brMcDowellhttps://www.blogger.com/profile/15240682237791734569noreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-14787335017748563552011-07-10T02:43:34.824+01:002011-07-10T02:43:34.824+01:00According to http://www.w3.org/2007/01/applets/xpa...According to http://www.w3.org/2007/01/applets/xpathApplet.html<br />/:data/foo:value is an invalid expression.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-27285071554485965572011-06-03T09:15:40.223+01:002011-06-03T09:15:40.223+01:00@Anonymous - I wasn't aware that implementatio...@Anonymous - I wasn't aware that implementations varied. I would be inclined to just namespace everything: "/xyz:data/abc:value"McDowellhttps://www.blogger.com/profile/15240682237791734569noreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-7991039397317866902011-06-02T19:08:43.808+01:002011-06-02T19:08:43.808+01:00Is there any way to use default namespace without ...Is there any way to use default namespace without using ":" (as the standard?) /data/foo:value instead of /:data/foo:value ?<br />I am using xalan 2.7.1 and doesn't work, and if I use saxon I got a Unexpected colon at start of tokenAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-32554444619811285392010-11-30T20:33:13.020+00:002010-11-30T20:33:13.020+00:00@Anonymous - anyone is free to use the sample code...@Anonymous - anyone is free to use the sample code in this post with the caveats noted at the bottom of the page.McDowellhttps://www.blogger.com/profile/15240682237791734569noreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-50620776879583535742010-11-30T17:09:05.362+00:002010-11-30T17:09:05.362+00:00Thanks for your post, it's a pity we cannot fi...Thanks for your post, it's a pity we cannot find any implementation of NamespaceContext provided in the standard library.<br /><br />Can I use your sample code for the NamespaceContextMap or is it protected by a copyright ?<br /><br />Thanks again and seeyaAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2083400580050165811.post-88894795957337602562010-01-27T11:27:58.680+00:002010-01-27T11:27:58.680+00:00Found a great impl: org.apache.ws.commons.util.Nam...Found a great impl: org.apache.ws.commons.util.NamespaceContextImpl.<br />You can use the following maven dependency for it:<br /> <br /> org.apache.ws.commons<br /> ws-commons-util<br /> 1.0.1<br /> testAsaf Mesikahttps://www.blogger.com/profile/01139334384842114609noreply@blogger.com