You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Lars Holmstrom <la...@thinkshare.com> on 2000/08/25 08:58:54 UTC

XPath Namespace queries on orphaned trees.

Hey there.  I am having trouble getting XPath queries to work when the
context node is part of a subtree that is not a descendent of the root node
of the document (this tree has been imported but not yet inserted into the
main document tree).  I am using the XpathAPI for performing these queries.
I know that the query execution for Namespace nodes relies on access to the
node where the Namespace attributes are defined.  I am calling the following
function in the XpathAPI:

  public static XObject eval(Node contextNode, String str, Node
namespaceNode)
    throws SAXException
  {
    // Since we don't have a XML Parser involved here, install some default
support
    // for things like namespaces, etc.
    // (Changed from: XPathSupportDefault xpathSupport = new
XPathSupportDefault();
    //    because XPathSupportDefault is weak in a number of areas...
perhaps
    //    XPathSupportDefault should be done away with.)
    XPathSupport xpathSupport = new XMLParserLiaisonDefault();

    if(null == namespaceNode)
      namespaceNode = contextNode;

    // Create an object to resolve namespace prefixes.
    // XPath namespaces are resolved from the input context node's document
element
    // if it is a root node, or else the current context node (for lack of a
better
    // resolution space, given the simplicity of this sample code).
    PrefixResolverDefault prefixResolver = new
PrefixResolverDefault((namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
                                                         ?
((Document)namespaceNode).getDocumentElement() :
                                                           namespaceNode);

    // Create the XPath object.
    XPath xpath = new XPath();

    // Create a XPath parser.
    XPathProcessorImpl parser = new XPathProcessorImpl(xpathSupport);
    parser.initXPath(xpath, str, prefixResolver);

    // Execute the XPath, and have it return the result
    return xpath.execute(xpathSupport, contextNode, prefixResolver);
  }

The contextNode is part of an orphaned tree.  The namespaceNode passed in is
the document root element of the document.  My Namespace attributes are set
on this element.  The string is a qualified element name and equals
"tsxul:foo".  The prefix resolver is working, because after the
parser.initXPath() method is called, the xpath has been tokenized to
"urn:thinkshare.com:tsxul, :, foo".  The execution of the query fails to
return the tsxul:foo element that is a child of the contextNode.  If I
insert the contextNode and its children anywhere in the main document tree,
the query returns the correct node.  If I set the
xmlns:tsxul="urn:thinkshare.com:tsxul" attribute on the contextNode (without
inserting it into the tree), the query also returns the correct node.  It
seems as if the internal processing depends on the ancestry of the
contextNode rather than the namespaceNode to resolve namespace prefixes at
some point.  I would appreciate any input with respect to this issue.  Thank
you.

Lars Holmstrom
ThinkShare