If you want to use the resulting xpath string with
org.apache.xpath.XPathAPI, and you ever use
xmlns="someURI"
you need to start the element names in that namespace with colon in XPath
expressions fed to XPathAPI.  I don't know why, but it's probably a faq.

Jeff
----- Original Message -----
From: "Dominique Devienne" <[EMAIL PROTECTED]>
To: "'Jeff Greif'" <[EMAIL PROTECTED]>; "Joseph Kesselman"
<[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Tuesday, November 26, 2002 1:17 PM
Subject: RE: xpath constructor


> Shouldn't you add the ':' only if prefix is non-null? --DD
>
> -----Original Message-----
> From: Jeff Greif [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, November 26, 2002 3:12 PM
> To: Jeff Greif; Joseph Kesselman
> Cc: [EMAIL PROTECTED]
> Subject: Re: xpath constructor
>
> This is a better solution not depending on anything but pure DOM, and
> possibly with better performance.  It could be improved if there is some
> kind of publicly accessible table of intern'ed strings in the DOM
> implementation.
>
>     public static String getPathFromRoot(Element e) {
>         Node p = e.getParentNode();
>         StringBuffer path = new StringBuffer();
>         try {
>             if (p.getNodeType() == Node.ELEMENT_NODE ) {
>                 path.append(getPathFromRoot((Element)p));
>             }
>             path.append("/");
>             String prefix = e.getPrefix();
>             String local = e.getLocalName();
>             String namespaceUri = e.getNamespaceURI();
>             path.append(prefix != null ? prefix : "");
>             path.append(":");
>             path.append(local);
>             int siblings = 0;
>             for (Node s=e.getPreviousSibling(); s != null;
>                  s=s.getPreviousSibling()) {
>                 if (s.getNodeType()== Node.ELEMENT_NODE
>                     && s.getLocalName().equals(local)
>                     && s.getNamespaceURI().equals(namespaceUri)) {
>                     ++siblings;
>                 }
>             }
>             path.append("[");
>             path.append(1 + siblings);
>             path.append("]");
>             return path.toString();
>
>         } catch (Exception ex) {
>             ex.printStackTrace(System.err);
>             System.exit(-63);
>         }
>         return "";
>     }
>

Reply via email to