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 19th, 2005, 05:50 AM
Authorized User
 
Join Date: Sep 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath Evaluate Document

Hi,
i need to use the xpath.evaluate method, but instead of passing an InputSource i'm trying to pass a Document (because a i want to do various evalutes, and it takes less time if i just pass the parsed Document)
I have this code

Document document = DocumentBuilderFactory.newInstance().newDocumentBu ilder().parse("rdf.xml");

String expr = "/rdf:RDF/rdf:Description/prf:component/rdf:Description/prf:CcppAccept/rdf:Bag/rdf:li='image/vnd.wap.wbmp'";

XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new TesteNameSpace(rdf));

String result = xpath.evaluate(expr,document);

The result is false, but it should be true
If i do the same thing, but instead of passing a Document, i pass an InputSource, it work perfectly
My problem is that it used to work with a document object, but now it doesn't and i didn't change a thing
Does anyone have a clue?
Thanks
 
Old September 19th, 2005, 06:01 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Have you provided mappings for the other all namespace URIs? The ones mapped to prefixes "rdf" and "prf".
Where is rdf defined in:
Code:
xpath.setNamespaceContext(new TesteNameSpace(rdf));
--

Joe (Microsoft MVP - XML)
 
Old September 19th, 2005, 07:29 AM
Authorized User
 
Join Date: Sep 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your post, but i finally found out what was wrong.
rdf is the first node it's like = document.getElementsByTagName("rdf:RDF").item(0);
(this isn't the actual code, because i check for erros and all, but it's the simplified version)
The uri mappings are given in the TestNameSpace, the construct read the xmlns attributes from the rdf node (explained above) and stores 3 variables, then when i need a prefix it does this
if(prefix.compareToIgnoreCase("rdf") == 0)
    return rdf;
else if(prefix.compareToIgnoreCase("prf") == 0)
    return prf;
else if(prefix.compareToIgnoreCase("mms") == 0)
    return mms;
return null;

Now for the resolution, actually it was quite simple, but i still don't quite understand the reason.
The problem was with the expression, in my code i had several expressions, for example
String expr = "rdf:RDF/rdf:Description/prf:component/rdf:Description/prf:Vendor/text()";

this worked fine with the InputSource, but with a document, i had to take out the prefixes, so i had to evaluate

String expr = "RDF/Description/component/Description/Vendor/text()";

And this worked






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to evaluate parameter? chobo XSLT 8 February 25th, 2008 07:05 AM
Evaluate string in the where clause mat41 SQL Language 5 February 1st, 2008 02:07 AM
Xpath error in Document() bonekrusher XSLT 13 September 30th, 2006 02:39 AM
XPath API and DOM Document object nnravi XML 10 May 2nd, 2006 12:24 PM
How do i evaluate A_josh VB How-To 1 December 22nd, 2003 01:47 PM





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