[xquery-talk] Populating Hash Table from Saxxon

Michael Kay mhk at mhk.me.uk
Mon Sep 13 11:57:14 PDT 2004

Your variable $c is bound to an element whose name is XMLID. You are then
trying to find an attribute of that element named @FIELDID. But your XMLID
elements do not have any attributes called FIELDID. Perhaps you meant @ID.
If you change @FIELDID to @ID, your results should look like 
<RESULT ID="F1_1C1_1"><XMLID ID="F1_1C1_1">101</XMLID></RESULT>
I suspect this isn't quite what you were looking for. If you want the value
of the element/attribute in your result, rather than a copy of the
element/attribute, use the string() function or the data() function to
extract the value of the node.
Incidentally, names starting with "XML" are reserved for future
standardisation, and some parsers give you warnings if you try to use them.
Michael Kay


From: talk-bounces at xquery.com [mailto:talk-bounces at xquery.com] On Behalf Of
sanjay vig
Sent: 13 September 2004 10:18
To: talk at xquery.com
Subject: RE: [xquery-talk] Populating Hash Table from Saxxon 

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

The following is the XML structure
<?xml version="1.0" encoding="UTF-8"?>
  <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>
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();
 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
     SequenceIterator books = exp.iterator(dynamicContext);
     while (true){
       NodeInfo book    = (NodeInfo)books.next();
      System.out.println("book ="+book);
       if (book==null) break;
       String title = book.getStringValue();
       String ID  =book.getAttributeValue(book.getFingerprint());
  }catch(Exception ex)
  } catch(Exception e) {


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


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 


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.



Sanjay Vig



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


Do you Yahoo!?
_mail/static/efficiency.html> and Improved Yahoo! Mail - Send 10MB messages!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://xquery.com/pipermail/talk/attachments/20040913/52a4f517/attachment-0001.htm

More information about the talk mailing list