[xquery-talk] Populating Hash Table from Saxxon

sanjay vig vigsanjay at yahoo.com
Mon Sep 13 03:18:12 PDT 2004


Hi,
 
We are using the following code for getting string values and attributes. But we are not able to get attributes. I am sending you the code that we have  written along with the xml we are using.
The query we are using is
  for $b  in  /XMLSAVE/DATAINFORMATION/SECTION  ,$c in $b/XMLID    return  <RESULT  > {$c/@FIELDID} {$c}  </RESULT>  
where c is the attribute value and b is value of the node.
I have marked the code in red where we are trying to get the attribute value.
 
The following is the XML structure
<?xml version="1.0" encoding="UTF-8"?>
 
<XMLSAVE>
 <DATAINFORMATION>
  <SECTION SECTIONID="Customer"  LISTID="List11"  >
    <XMLID ID="F1_1C1_1">101</XMLID>
    <XMLID ID="F1_1C1_2">Induttori</XMLID>
    <XMLID ID="F1_1C1_3">Grugliasco</XMLID>
    <XMLID ID="F1_1C1_4">Itali</XMLID>
    <XMLID ID="F1_1C1_5">Paolo</XMLID>
    <XMLID ID="F1_1C2_1">102</XMLID>
    <XMLID ID="F1_1C2_2">Technical Specialists Co.</XMLID>
    <XMLID ID="F1_1C2_3">New Haven</XMLID>
    <XMLID ID="F1_1C2_4">USA</XMLID>
    <XMLID ID="F1_1C2_5">Leslie</XMLID>
    <XMLID ID="F1_1C3_1">106</XMLID>
    <XMLID ID="F1_1C3_2">Technical MicroSystems Inc.</XMLID>
    <XMLID ID="F1_1C3_3">NYC</XMLID>
    <XMLID ID="F1_1C3_4">USA</XMLID>
    <XMLID ID="F1_1C3_5">Jeff</XMLID>
   </SECTION>
  </DATAINFORMATION>
 </XMLSAVE>
------------------------------------------------------------------------------Code------------------------------------------------------------------------------------------------

package com.ReportTool.xml;
import net.sf.saxon.tree .*;
import net.sf.saxon.xpath.XPathEvaluator;
import net.sf.saxon.xpath.XPathExpression;
import net.sf.saxon.xpath.Variable;
import net.sf.saxon.xpath.StandaloneContext;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.Item;
import net.sf.saxon.*;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.QueryProcessor;
import net.sf.saxon.query.XQueryExpression;
import net.sf.saxon.query.*;
import java.io.*;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.om.*;
import javax.xml.transform.stream.StreamResult;

class XMLQuery {
 
 public static void main(String[] args)
 {
 
  XMLQuery query = new XMLQuery();
 
  query.getXMLResult();
 
 }
 
 QueryProcessor xquery = null;
 Configuration config = null;
 StaticQueryContext staticContext = null;
 
 public XMLQuery() {
 
  config = new Configuration();
  staticContext = new StaticQueryContext(config);
  xquery = new QueryProcessor(staticContext);
 
 }
 
 
 
 public void getXMLResult()
 {
 
  try {
 
     String path = "bib.xml";
     String Query="   for $b  in  /XMLSAVE/DATAINFORMATION/SECTION  ,$c in $b/XMLID    return  <RESULT  > {$c/@FIELDID} {$c}  </RESULT>    ";
     XQueryExpression exp = xquery.compileQuery(Query);
     System.out.println("thhhhhasfdgsdfg  "+exp+"The Queryyyy "+Query);
     DynamicQueryContext dynamicContext = new DynamicQueryContext();
     dynamicContext.setContextNode(xquery.buildDocument(new StreamSource(new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\Root\\XML\\CustomerDetails_2813_2002-12-31_11_186_1_V1.xml"))));
     SequenceIterator books = exp.iterator(dynamicContext);
  
     while (true){
       NodeInfo book    = (NodeInfo)books.next();
 
      System.out.println("book ="+book);
       if (book==null) break;
       System.out.println(book.getDisplayName());
       String title = book.getStringValue();
       String ID  =book.getAttributeValue(book.getFingerprint());
       System.out.println("title= "+title+"::"+ID+"::"+book.getFingerprint()+"type"+book.getNodeKind());
        }
 
  try{
 
   QueryResult.wrap(books,config);
  }catch(Exception ex)
  {
   ex.printStackTrace();
  }
  } catch(Exception e) {
 
   e.printStackTrace();
  }
 }
}

 


Michael Kay <mhk at mhk.me.uk> wrote:It's probably best to ask Saxon-specific questions over on the saxon-help list sourceforge.net.
 
It's hard to see what you're doing wrong from the information you've given. It could be that the query itself is wrong, or it could be that you're processing the results incorrectly. If you want to get the query results into a Java program, the best way is to use the iterator() method on the XQueryExpression, to get a SequenceIterator over the results; you then call the next() method repeatedly on the SequenceIterator until it returns null. The next() method returns an Item which will be either a NodeInfo or an AtomicValue; if you are confident that your query is only retrieving nodes, then you can cast the Item to a NodeInfo.
 
I'm not sure exactly what you want the key and value in your hash table to be. You can get the string value of the node by calling its getStringValue() method. You can also get its typed value using the getTypedValue() method. In the general case the typed value will be a sequence of atomic values (though in the non-schema-aware version of the product it will always be a sequence of length one). It's not clear what you want to hold as the "id" of the node. Saxon is able to generate an identifier for each node using the NodeInfo#generateId() method (which supports XSLT's generate-id() function), but it's of limited use because you can't use the id to find the node.
 
I'm developing a set of example use cases for the Saxon Query API which I hope to include in the next software release, hopefully that will make things easier to understand.
 
Michael Kay
http://www.saxonica.com/


---------------------------------
From: talk-bounces at xquery.com [mailto:talk-bounces at xquery.com] On Behalf Of sanjay vig
Sent: 11 September 2004 06:38
To: talk at xquery.com
Subject: [xquery-talk] Populating Hash Table from Saxxon 





Hi, 

I am using Saxxon processor for using xquery on xml.

I am trying to populate hashtable out of the resultvalue from nodeinfo method but i am getting only a single string therefore I am not able to populate the hashtable.

I want to get id and value and each node in xml file.

 

Regards,

Sanjay Vig

 


---------------------------------
Do you Yahoo!?
Shop for Back-to-School deals on Yahoo! Shopping.

		
---------------------------------
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://xquery.com/pipermail/talk/attachments/20040913/243b08aa/attachment.htm


More information about the talk mailing list