You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by "Ian Sollars (JIRA)" <xa...@xml.apache.org> on 2005/06/03 14:11:01 UTC

[jira] Commented: (XALANJ-2138) String literals passed to XPathAPI result in XPathException

    [ http://issues.apache.org/jira/browse/XALANJ-2138?page=comments#action_67004 ] 

Ian Sollars commented on XALANJ-2138:
-------------------------------------

This is my quick and dirty fix to XPathAPI.selectNodeIterator(). I imagine for the actual fix that you wouldn't resort to using an anonymous inner class.

Regards,

 - Ian

  /**
   *  Use an XPath string to select a nodelist.
   *  XPath namespace prefixes are resolved from the namespaceNode.
   *
   *  @param contextNode The node to start searching from.
   *  @param str A valid XPath string.
   *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
   *  @return A NodeIterator, should never be null.
   *
   * @throws TransformerException
   */
  public static NodeIterator selectNodeIterator(
          final Node contextNode, String str, Node namespaceNode)
            throws TransformerException
  {

    // Execute the XPath, and have it return the result
    final XObject list = eval(contextNode, str, namespaceNode);
	
	if( list.getType() == XObject.CLASS_NUMBER ||
		list.getType() == XObject.CLASS_STRING ) {
			return new NodeIterator() {
				
				private Node textNode = contextNode.getOwnerDocument().createTextNode(list.str());

				public int getWhatToShow() {
					return NodeFilter.SHOW_TEXT;
				}

				public void detach() {
					textNode = null;					
				}

				public boolean getExpandEntityReferences() {
					return false;
				}

				public Node getRoot() {
					return null;
				}

				public Node nextNode() throws DOMException {
					Node result = textNode;
					textNode = null;
					return result;
				}

				public Node previousNode() throws DOMException {
					return null;
				}

				public NodeFilter getFilter() {
					return null;
				}
				
			};
	}
		

    // Have the XObject return its result as a NodeSetDTM.                
    return list.nodeset();
  }


> String literals passed to XPathAPI result in XPathException
> -----------------------------------------------------------
>
>          Key: XALANJ-2138
>          URL: http://issues.apache.org/jira/browse/XALANJ-2138
>      Project: XalanJ2
>         Type: Bug
>   Components: XPath-API
>     Versions: 2.6
>  Environment: WinXP sp2 Prof, J2SDK1.4.2_08, Eclipse 3.1Mx, Xalan 2.6.0
>     Reporter: Ian Sollars

>
> The following is a valid XPath expression:
> 'test'
> However, code such as the following:
> 		Document d = XMLUtils.newDocument(new StringInputStream("<xml></xml>"));
> 		
> 		System.out.println(  XPathAPI.selectSingleNode(d.getDocumentElement(), "'test'")  );
> produces the following stack trace:
> org.apache.xpath.XPathException: Can not convert #STRING to a NodeList!
> 	at org.apache.xpath.objects.XObject.error(XObject.java:741)
> 	at org.apache.xpath.objects.XObject.nodeset(XObject.java:471)
> 	at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:175)
> 	at org.apache.xpath.XPathAPI.selectSingleNode(XPathAPI.java:133)
> 	at org.apache.xpath.XPathAPI.selectSingleNode(XPathAPI.java:113)
> [jUnit & Eclipse lines elided]
> My suggestion is to change XPathAPI.selectNodeIterator (line 166 in 2.6.0) to test getType() against CLASS_STRING and CLASS_NUMBER(?), and return contextNode.getOwnerDocument().createTextNode(list.str()) if equal to one of those two.
> Thanks a lot,
>  - Ian

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org