6 Replies Latest reply: Jan 13, 2007 4:54 PM by 807607 RSS

    XPath getting attribute problem....

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

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

      <TEST/>

      </BLABLA>

      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....
          807607
          XPath xpathSelector = DocumentHelper.createXPath(XPATH); doesn't change anything
          • 2. Re: XPath getting attribute problem....
            807607
            //BLABLA
            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....
              DrClap
              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....
                807607
                I have one more question, it may be some other problem - i've noticed something:

                I have an XML file
                <request>
                <mustbe>
                <SSS type="mustbe" WWW="dada.com">
                 <SSS2 type2="mustbe" WWW2="dada.com">
                      
                 </SSS2>
                     
                     </SSS>
                </mustbe>
                </request>
                And the code that doesn't work..
                InputStream is = getClass()
                                    .getResourceAsStream("/Sample.xml");
                
                          SAXReader reader = new SAXReader();
                          try {
                               Document document = reader.read(is);
                               //Node node=document;
                               Node node = document.getRootElement().element("mustbe").element("SSS").detach();
                               
                               
                               System.out.println(node.asXML());
                               
                               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);
                               
                               System.out.println("Works"+attr.asXML());
                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:
                herbatniczek
                • 5. Re: XPath getting attribute problem....
                  807607
                  Try "//SSS/@WWW".
                  • 6. Re: XPath getting attribute problem....
                    807607
                    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:
                    "self::node()/@WWW"
                    "./@WWW"
                    "@WWW"