6 Replies Latest reply on Jan 13, 2007 10:54 PM by 807607

    XPath getting attribute problem....

      I have a dom4j Node which asXml() method gives u

      <BLABLA type="content" attr1="valueofAAA">



      public static final String XPATH = "/*[name()='BLABLA']/@attr1";
      XPath xpathSelector = DocumentHelper.createXPath("XPATH");
      List listtt=xpathSelector.selectNodes(node.getDocument());

      The listtt is always empty.... why?

      PS. I've also tried
      XPath xpathSelector = DocumentHelper.createXPath("//BLABLA");
      XPath xpathSelector = DocumentHelper.createXPath("BLABLA");
      XPath xpathSelector = DocumentHelper.createXPath(".//BLABLA");

      The list is always empty..... can u correct it?
        • 1. Re: XPath getting attribute problem....
          XPath xpathSelector = DocumentHelper.createXPath(XPATH); doesn't change anything
          • 2. Re: XPath getting attribute problem....
            should work. I suggest you raise the issue in the support forum of whichever third party library it is you're using, as DocumentHelper isn't in the standard libraries.
            • 3. Re: XPath getting attribute problem....
              Except when the element is in the default namespace (which we can't tell from the original post). XPath 1.0 can't identify elements by name if they are in the default namespace, and the only way I know of to deal with this is to use the local-name() function instead of the name() function in the original example posted.
              • 4. Re: XPath getting attribute problem....
                I have one more question, it may be some other problem - i've noticed something:

                I have an XML file
                <SSS type="mustbe" WWW="dada.com">
                 <SSS2 type2="mustbe" WWW2="dada.com">
                And the code that doesn't work..
                InputStream is = getClass()
                          SAXReader reader = new SAXReader();
                          try {
                               Document document = reader.read(is);
                               //Node node=document;
                               Node node = document.getRootElement().element("mustbe").element("SSS").detach();
                               String XPATH = "/*[name()='SSS']/@WWW";
                               XPath xpathSelector = DocumentFactory.getInstance().createXPath(XPATH);//DocumentHelper.createXPath(XPATH);
                               List listtt=xpathSelector.selectNodes(node);
                               Attribute attr=(Attribute) listtt.get(0);
                And this is the real thing that i have in my code except for name of nodes. Maybe this would tell u something/

                Message was edited by:
                • 5. Re: XPath getting attribute problem....
                  Try "//SSS/@WWW".
                  • 6. Re: XPath getting attribute problem....
                    It seems that the "/*" axis can only be used when the node passed to the selectNodes() method was identical with the root element of the document at the time of parsing. If your node was one of the inner nodes, you can only refer to it with "self::node()", "." or nothing at all, the last meaning that the query string starts immediately with a location step other than that of the context node. To illustrate this, here are three interchangeable query examples that can make your code work properly: