?
Solved

Java XPath namespace

Posted on 2007-07-23
13
Medium Priority
?
1,058 Views
Last Modified: 2013-11-18
I have a XML schema that I get from a third party. I need to do some XPATH queries on it but it doesnt have xmlns: defined i.e there is no namespace information . I have tried many java implentations JAXP, JDOM etc. I havent had any luck find results to my XPath queries. Any suggestions
0
Comment
Question by:mostenscer
  • 7
  • 6
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19546870
You mean you want to query metadata?
0
 
LVL 2

Author Comment

by:mostenscer
ID: 19547128
no, I need to query records. for instance.
want to get
/OAI-PMH/Listrecords/recrod[1]/heards/identfier/text()
and i expect the result to be like
oai:depot.cnx.abc.org:8080:m0102

 here is the simplified form  of the xml file
----------------------------------------------------------
      -<OAI-PMH xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/                              http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>2007-07-23T12:18:35Z</responseDate>
<request metadataPrefix="ims1_2_1" verb="ListRecords">http://depot.cnx.rice.edu:8080/content/OAI</request>

      <ListRecords>

      <record>

      <header>
<identifier>oai:depot.cnx.abc.org:8080:m0102</identifier>
<datestamp>2005-04-13T21:39:12Z</datestamp>
</header>
      </record>

<record>

      <header>
<identifier>oai:depot.cnx.abc.net:8080:m0102</identifier>
<datestamp>2005-04-13T21:39:12Z</datestamp>
</header>
      </record>
<record>

      <header>
<identifier>oai:depot.cnx.abc.com:8080:m0102</identifier>
<datestamp>2005-04-13T21:39:12Z</datestamp>
</header>
      </record>
<record>

      <header>
<identifier>oai:depot.cnx.abc.edu:8080:m0102</identifier>
<datestamp>2005-04-13T21:39:12Z</datestamp>
</header>
      </record>
    </ListRecords>

</OAI-PMH>
0
 
LVL 2

Author Comment

by:mostenscer
ID: 19547137
please ignore the case insensitiveness of my writing:)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 19547186
>>I have a XML schema that I get from a third party

OK - but what you've just posted is not a schema but an xml file. Have you tried XPath on it?
0
 
LVL 2

Author Comment

by:mostenscer
ID: 19547202
yes,  some how  i get no results. since there is no namespace i "guess"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19547251
//identifier/text()

works fine for me, giving:

oai:depot.cnx.abc.org:8080:m0102
oai:depot.cnx.abc.net:8080:m0102
oai:depot.cnx.abc.com:8080:m0102
oai:depot.cnx.abc.edu:8080:m0102
0
 
LVL 2

Author Comment

by:mostenscer
ID: 19547329
could you paste the  skeleton  code here plz ?
 
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 19547377
XPath xpath = XPathFactory.newInstance().newXPath();
// Evaluate the document against the pattern
NodeList nodeList = (NodeList) xpath.evaluate("//identifier/text()", document, XPathConstants.NODESET);
if (nodeList.getLength() > 0) {
      for(int i = 0;i < nodeList.getLength();i++) {
            System.out.println(nodeList.item(i).getNodeValue());
      }
}      
0
 
LVL 2

Author Comment

by:mostenscer
ID: 19548112
what api are you using ? singnature for api are pretty confusing thats why i am asking
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19548171
The JDK only
0
 
LVL 2

Author Comment

by:mostenscer
ID: 19548263
i am pretty new to XPath stuff so cant figure out how to resovlve the imports and may there is some other interface implementation i am missing
here is my code which is missing some imports
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.ParsingException;

import org.w3c.dom.NodeList;

import com.sun.org.apache.xerces.internal.impl.xpath.XPath;


public class XPathTest {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
      
          
          Document doc=null;
          try {
                Builder parser = new Builder();
                doc = parser.build("depot.cnx.rice.edu:8080/content/OAI?verb=SearchRecords&metadataPrefix=ims1_2_1&query:list=dsp%20and%20C%20language&b_size=-1");
              }
              catch (ParsingException ex) {
                System.err.println("Document cannot be parsed!");
              }
              catch (IOException ex) {
                    ex.printStackTrace();
                System.err.println("Could not establish an internet connections.  The site may be down.");
                

      }

              
             XPath xpath = XPathFactory.newInstance().newXPath();
          
          NodeList nodeList = (NodeList) xpath.
          ("//identifier/text()", doc, XPathConstants.NODESET);
            (nodeList.getLength() > 0) {
              for(int i = 0;i < nodeList.getLength();i++) {
                     System.out.println(nodeList.item(i).getNodeValue());
              
                
}
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19548324
No - don't mix librararies or you're going to be in trouble. Just use the JDK
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19549315
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question