Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2005, 12:07 PM
Authorized User
 
Join Date: Sep 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Java & XPath

Hi,
i am trying to use XPath in a Java application, but i have a problem, this is better with an example, i have the following code

XPath xpath = XPathFactory.newInstance().newXPath();
String expr = "//a/b/text()";
InputSource inputSource = new InputSource("teste.xml");
String result = xpath.evaluate(expr,inputSource);
System.out.println(result);

and the following XML file

<a>
    <b>hi</b>
</a>

And this works fine, i get the print of hi

The problem is when the XML file uses a namespace like this

<prx:a xlmns:prx=WHATEVER>
      <prx:b>hi</prx:b>
</prx:a>

the code above (naturally) doesn't work, the problem is even if i change expression in the code above to

String expr = "//prx:a/prx:b/text()";

It still doesn't work, i don't get an error, but i get an empty string printed on the Console

Do you have any ideia of whats this all about? And how can a fix this

thank you

 
Old September 14th, 2005, 12:37 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to do

xpath.setNamespaceContext(new XXX())

and

class XXX implements NamespaceContext {

String getNamespaceURI(String prefix) {
  if (prefix.equals("prx")) {
return "WHATEVER"
}}

The NamespaceContext also needs another couple of methods but I suspect the XPath engine won't call them (it won't if your chosen XPath engine is Saxon).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 15th, 2005, 04:42 AM
Authorized User
 
Join Date: Sep 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all thank you for your answer,
i tried it and i works fine,
but is there anyway (like another Class or something) that i can just pass the expression without creating a namespace and everything.
Because i'm trying to parse the XML that is passed to me by a user, so i don't know if it will have any other namespace then the ones i need.

I know i can do this with Jakarta Tags X-Lib, like this:
<xtags:variable id="brand" select="/prx:a/prx:b/text()"/>
And with out specifing the namespace i get the right result.
But i can't use this because i'm not working in a JSP file

So if there is another way i would appreciate it
Thanks





 
Old September 15th, 2005, 05:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XPath, the namespace of an element is part of its name. Not knowing the namespace is like not knowing the local name. There are still ways of finding elements without knowing their names, for example you can use //*[local-name()='xyz'], but you're very much working in the dark.

What you don't need to know is the namespace prefix. It's the namespace URI that matters; your XPath expression should be independent of the prefix used in the source document, which users can choose at will.

Saxon does allow you to construct a NamespaceContext object from a node in the source document (for example, the document element). Specifically, you can do

NamespaceResolver nr = new NamespaceResolver(node);
NamespaceContext nc = new NamespaceContextImpl(nr);

and if you pass this NamespaceContext to the xpath engine, your XPath expression can then use all the namespace bindings that are in scope for that node. However, this is specific to the Saxon implementation of the JAXP XPath API. The reason for this feature is primarily because it allows XPath expressions to be used (like XPointers) as hyperlinks within a document. It's not a good idea to write application code that depends on knowing the prefixes used in the source document but not knowing the URIs - that's not the way namespaces are intended to be used.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 15th, 2005, 06:04 AM
Authorized User
 
Join Date: Sep 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think i understand what you are saying
Thank you for your replys






Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath & XSLT problem Andy-7M XSLT 7 May 3rd, 2007 02:50 AM
java.lang.ClassCastException: org.apache.xpath.obj ksskumar XSLT 4 December 15th, 2006 03:13 AM
XSL:CHOOSE & XPATH falmouth XSLT 3 November 17th, 2004 08:36 AM
Browsing DOM in Java - Xpath? holdmykidney XML 2 October 4th, 2004 09:08 AM
Java & Xml mehdi_979 J2EE 1 May 15th, 2004 06:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.