You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Vladimir (JIRA)" <ji...@apache.org> on 2007/08/16 23:49:30 UTC

[jira] Created: (JXPATH-99) JXPath works incorrectly with CyberNeko HtmlParser

JXPath works incorrectly with CyberNeko HtmlParser
--------------------------------------------------

                 Key: JXPATH-99
                 URL: https://issues.apache.org/jira/browse/JXPATH-99
             Project: Commons JXPath
          Issue Type: Bug
    Affects Versions: 1.2 Final
            Reporter: Vladimir


I don't know exactly where is the bug. I have an idea, that CyberNeko html parser creates some wired w3c DOM representation of html file, and that is the cause. Here is a code sample:
// ---------------------------
       // create CyberNeko html parser 
        DOMParser parser = new DOMParser();
        // this page does have //input[@name='q'] field
        parser.parse("http://google.com");
        Document doc = parser.getDocument();

        // JXPATH TEST
        JXPathContext context = JXPathContext.newContext(doc);
        List nodes1 = context.selectNodes("//input[@name='q']"); // ERROR IS HERE: call returns nothing, must return 1 node
        List nodes2 = context.selectNodes("//*"); // returnes 78 nodes
        System.out.println(nodes1.toString());
        System.out.println(nodes2.toString());

        // XPathFactory TEST  ( for comparison )
        // error, returns nothing
        Object list1 = XPathFactory.newInstance().newXPath().compile("//input[@name='q']").evaluate(doc, XPathConstants.NODESET);
        // returns 79 nodes
        Object list2 = XPathFactory.newInstance().newXPath().compile("//*").evaluate(doc, XPathConstants.NODESET);
        System.out.println(list1);
        System.out.println(list2);
// -----------------------------------

Is it possible to fix this problem inside JXPath? Or is it only html parser problem?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Resolved: (JXPATH-99) JXPath works incorrectly with CyberNeko HtmlParser

Posted by "Matt Benson (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/JXPATH-99?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Matt Benson resolved JXPATH-99.
-------------------------------

    Resolution: Invalid

The problem here is that the Neko parser you are using specifies a particular Xerces HTML document implementation that converts all element names to upper case.  Thus in your example you should have been searching for //INPUT[@name='q'] .  As for your followup additional method to search by attribute only that seems to be equivalent to e.g. //*[@name='q'] .

-Matt

> JXPath works incorrectly with CyberNeko HtmlParser
> --------------------------------------------------
>
>                 Key: JXPATH-99
>                 URL: https://issues.apache.org/jira/browse/JXPATH-99
>             Project: Commons JXPath
>          Issue Type: Bug
>    Affects Versions: 1.2 Final
>            Reporter: Vladimir
>
> I don't know exactly where is the bug. I have an idea, that CyberNeko html parser creates some wired w3c DOM representation of html file, and that is the cause. Here is a code sample:
> // ---------------------------
>        // create CyberNeko html parser 
>         DOMParser parser = new DOMParser();
>         // this page does have //input[@name='q'] field
>         parser.parse("http://google.com");
>         Document doc = parser.getDocument();
>         // JXPATH TEST
>         JXPathContext context = JXPathContext.newContext(doc);
>         List nodes1 = context.selectNodes("//input[@name='q']"); // ERROR IS HERE: call returns nothing, must return 1 node
>         List nodes2 = context.selectNodes("//*"); // returnes 78 nodes
>         System.out.println(nodes1.toString());
>         System.out.println(nodes2.toString());
>         // XPathFactory TEST  ( for comparison )
>         // error, returns nothing
>         Object list1 = XPathFactory.newInstance().newXPath().compile("//input[@name='q']").evaluate(doc, XPathConstants.NODESET);
>         // returns 79 nodes
>         Object list2 = XPathFactory.newInstance().newXPath().compile("//*").evaluate(doc, XPathConstants.NODESET);
>         System.out.println(list1);
>         System.out.println(list2);
> // -----------------------------------
> Is it possible to fix this problem inside JXPath? Or is it only html parser problem?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (JXPATH-99) JXPath works incorrectly with CyberNeko HtmlParser

Posted by "Vladimir (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/JXPATH-99?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12520517 ] 

Vladimir commented on JXPATH-99:
--------------------------------

I've added a method, that searches nodes with specified attribute values through the document tree, and it returnes desired node.
//-------------
// call for the above example:
findByAttr(doc, "name", "q"); // similar to  //*[@name='q']

//------------
    private static Node findByAttr(Node node,String name,  String s) {
        if (node instanceof Element) {
            Element el = (Element) node;
            if (el.getAttribute(name).equals(s)) {
                return el;
            }
        }
        for (int i=0; i<node.getChildNodes().getLength(); i++) {
            Node result = findByAttr(node.getChildNodes().item(i),name,  s);
            if (result!=null) return result;
        }
        return null;
    }
//-----------


> JXPath works incorrectly with CyberNeko HtmlParser
> --------------------------------------------------
>
>                 Key: JXPATH-99
>                 URL: https://issues.apache.org/jira/browse/JXPATH-99
>             Project: Commons JXPath
>          Issue Type: Bug
>    Affects Versions: 1.2 Final
>            Reporter: Vladimir
>
> I don't know exactly where is the bug. I have an idea, that CyberNeko html parser creates some wired w3c DOM representation of html file, and that is the cause. Here is a code sample:
> // ---------------------------
>        // create CyberNeko html parser 
>         DOMParser parser = new DOMParser();
>         // this page does have //input[@name='q'] field
>         parser.parse("http://google.com");
>         Document doc = parser.getDocument();
>         // JXPATH TEST
>         JXPathContext context = JXPathContext.newContext(doc);
>         List nodes1 = context.selectNodes("//input[@name='q']"); // ERROR IS HERE: call returns nothing, must return 1 node
>         List nodes2 = context.selectNodes("//*"); // returnes 78 nodes
>         System.out.println(nodes1.toString());
>         System.out.println(nodes2.toString());
>         // XPathFactory TEST  ( for comparison )
>         // error, returns nothing
>         Object list1 = XPathFactory.newInstance().newXPath().compile("//input[@name='q']").evaluate(doc, XPathConstants.NODESET);
>         // returns 79 nodes
>         Object list2 = XPathFactory.newInstance().newXPath().compile("//*").evaluate(doc, XPathConstants.NODESET);
>         System.out.println(list1);
>         System.out.println(list2);
> // -----------------------------------
> Is it possible to fix this problem inside JXPath? Or is it only html parser problem?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (JXPATH-99) JXPath works incorrectly with CyberNeko HtmlParser

Posted by "Matt Benson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/JXPATH-99?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12551853 ] 

Matt Benson commented on JXPATH-99:
-----------------------------------

One more suggestion here:  I noticed this FAQ on the Neko site:

http://nekohtml.sourceforge.net/faq.html#uppercase


> JXPath works incorrectly with CyberNeko HtmlParser
> --------------------------------------------------
>
>                 Key: JXPATH-99
>                 URL: https://issues.apache.org/jira/browse/JXPATH-99
>             Project: Commons JXPath
>          Issue Type: Bug
>    Affects Versions: 1.2 Final
>            Reporter: Vladimir
>
> I don't know exactly where is the bug. I have an idea, that CyberNeko html parser creates some wired w3c DOM representation of html file, and that is the cause. Here is a code sample:
> // ---------------------------
>        // create CyberNeko html parser 
>         DOMParser parser = new DOMParser();
>         // this page does have //input[@name='q'] field
>         parser.parse("http://google.com");
>         Document doc = parser.getDocument();
>         // JXPATH TEST
>         JXPathContext context = JXPathContext.newContext(doc);
>         List nodes1 = context.selectNodes("//input[@name='q']"); // ERROR IS HERE: call returns nothing, must return 1 node
>         List nodes2 = context.selectNodes("//*"); // returnes 78 nodes
>         System.out.println(nodes1.toString());
>         System.out.println(nodes2.toString());
>         // XPathFactory TEST  ( for comparison )
>         // error, returns nothing
>         Object list1 = XPathFactory.newInstance().newXPath().compile("//input[@name='q']").evaluate(doc, XPathConstants.NODESET);
>         // returns 79 nodes
>         Object list2 = XPathFactory.newInstance().newXPath().compile("//*").evaluate(doc, XPathConstants.NODESET);
>         System.out.println(list1);
>         System.out.println(list2);
> // -----------------------------------
> Is it possible to fix this problem inside JXPath? Or is it only html parser problem?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (JXPATH-99) JXPath works incorrectly with CyberNeko HtmlParser

Posted by "Vladimir (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/JXPATH-99?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12551857 ] 

Vladimir commented on JXPATH-99:
--------------------------------

thank you, this was helpful!

> JXPath works incorrectly with CyberNeko HtmlParser
> --------------------------------------------------
>
>                 Key: JXPATH-99
>                 URL: https://issues.apache.org/jira/browse/JXPATH-99
>             Project: Commons JXPath
>          Issue Type: Bug
>    Affects Versions: 1.2 Final
>            Reporter: Vladimir
>
> I don't know exactly where is the bug. I have an idea, that CyberNeko html parser creates some wired w3c DOM representation of html file, and that is the cause. Here is a code sample:
> // ---------------------------
>        // create CyberNeko html parser 
>         DOMParser parser = new DOMParser();
>         // this page does have //input[@name='q'] field
>         parser.parse("http://google.com");
>         Document doc = parser.getDocument();
>         // JXPATH TEST
>         JXPathContext context = JXPathContext.newContext(doc);
>         List nodes1 = context.selectNodes("//input[@name='q']"); // ERROR IS HERE: call returns nothing, must return 1 node
>         List nodes2 = context.selectNodes("//*"); // returnes 78 nodes
>         System.out.println(nodes1.toString());
>         System.out.println(nodes2.toString());
>         // XPathFactory TEST  ( for comparison )
>         // error, returns nothing
>         Object list1 = XPathFactory.newInstance().newXPath().compile("//input[@name='q']").evaluate(doc, XPathConstants.NODESET);
>         // returns 79 nodes
>         Object list2 = XPathFactory.newInstance().newXPath().compile("//*").evaluate(doc, XPathConstants.NODESET);
>         System.out.println(list1);
>         System.out.println(list2);
> // -----------------------------------
> Is it possible to fix this problem inside JXPath? Or is it only html parser problem?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.