There are many implementations of the NodeInfo interface, and it's best in your code to make no assumptions about which implementation is being used. The net.sf.saxon.tree.NodeImpl version will be used if you select the "linked tree" (also called "standard tree"), but the default implementation is the "tinytree". Other implementations such as Orphan or TextFragmentImpl might be used for nodes created transiently during the course of a query or transformation. If the NodeInfo is implemented by a TinyTree then casting the node to NodeImpl will fail.
 
The way to get the next sibling using the NodeInfo interface (regardless of which tree implementation is used) is to do
 
SequenceIterator precedingSiblings = node.iterateAxis(Axis.PRECEDING_SIBLING);
while (true) {
  NodeInfo sibling = precedingSiblings.next();
  if (sibling == null) {
    // no sibling found
  }
  .. process the sibling
}
 
An alternative in this kind of scenario is to do the navigation by evaluating XPath expressions.
 
Hope this helps.
 
Michael Kay
http://www.saxonica.com


From: saxon-help-admin@lists.sourceforge.net [mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of Hou Ray
Sent: 07 April 2006 10:15
To: saxon-help@lists.sourceforge.net
Subject: [saxon] NodeImpl--howto

Hi, helpers

Recently I've downloaded Saxonb8-7j, because it implements XPath well. But when using it, I encountered a problem.
After I using an XPathExpression to get a List of elements, I can cast them to NodeInfo, that's OK. But I noticed that, in Saxon, there's a class net.sf.saxon.tree.NodeImpl, which has the method such as getNextSibling(). How can I use this class based on the NodeInfo I got previously? It seems that each time I wanna use it, I keep getting the error message:
java.lang.ClassCastException : net.sf.saxon.tinytree.TinyElementImpl      How can I handle it?

Detailed implementation is as following:
List   aList = myXPathExpression.evaluate(myDynamicQueryContext);
for (int i=0; i<saList.size();i++)
{
    NodeInfo nodeInfo = (NodeInfo)aList.get(i);  //that's OK
    NodeImpl nodeImpl =(NodeImpl)aList.get(i); //where I got ClassCastException
}

Is there anything I missed or did I use it in a wrong way? How can I fix it?

Great many thanks!!

--Rachel