Copy XPath of an AuthorNode

<oXygen/> general issues.
Posts: 206
Location: Hamburg, Germany

Copy XPath of an AuthorNode

Thu Jul 17, 2014 1:21 pm


we are using the Eclipse version of the Oxygen Author 15.2.

Last time of wanted to know how to get the plain xml of an AuthorNode.
Now I´d like to generate the XPath of a certain AuthorNode with the oXygen API.
In the standalone oXygen Author you have this "XPath toolbar", where the XPath can be shown.

Unfortunately I cannot find a method for getting the XPath in the AuthorDocumentController or somewhere else.

I know I could traverse the structure of a certain AuthorNode and generate a XPath expression myself, but as I saw the "XPath toolbar" I thought you may have a convenient method for that.

Looking forward to see the full qualified name of such a method :wink:

Best regards,

Simon Scholz
vogella GmbH
Posts: 604

Re: Copy XPath of an AuthorNode

Thu Jul 17, 2014 3:01 pm

Hello Simon,

Unfortunately we don't have such a method in the API but just like you've mentioned you can computed this expression using the existing API. To help you little, here is how we do it ourselves (I had to replace some private code with API so you should test it yourself in case I've missed anything):

Code: Select all

  public static String getXpathExpresion_(
        AuthorAccess authorAccess,
        boolean includeIndexInParent) throws BadLocationException {
     AuthorDocument doc = authorAccess.getDocumentController().getAuthorDocumentNode();
     int caretPos =  authorAccess.getEditorAccess().getCaretOffset();
     String toReturn = "";
     if (doc != null) {
        AuthorNode currentNode = authorAccess.getDocumentController().getNodeAtOffset(caretPos);
        // The proxy to namespace mapping from the root, build only one time.
        Map rootProxyToNsMap = null;
        while (currentNode != null && AuthorNode.NODE_TYPE_DOCUMENT != currentNode.getType()) {
           // Traverse the ancestors of the current ti and build xpath expr.
           String name = currentNode.getName();
           String proxy =  XmlUtil.getProxy(name);
           String localname = XmlUtil.getLocalName(name);
           // Find the index of current element in the parent
           String indexInParent = "";
           if (includeIndexInParent) {
              AuthorParentNode parent = (AuthorParentNode) currentNode.getParent();
              if (parent != null && AuthorNode.NODE_TYPE_DOCUMENT != parent.getType()) {
                 List children  = parent.getContentNodes();
                 int index = 0;
                 for (Iterator iter = children.iterator(); iter.hasNext();) {
                    AuthorNode child = (AuthorNode);
                    if (name.equals(child.getName())) {
                       index ++;
                       if (child == currentNode) {
                          indexInParent = "[" + index + "]";

           // Adds the XPath section corresponding to the current token item.
           if (proxy != null && localname != null) {
              switch (currentNode.getType()) {
              case AuthorNode.NODE_TYPE_REFERENCE:
                 toReturn = "";
              case AuthorNode.NODE_TYPE_ELEMENT:
                 toReturn = "/" + currentNode.getDisplayName() + indexInParent + toReturn;
              case AuthorNode.NODE_TYPE_COMMENT:
                 // The token is comment.
                 toReturn = "/" + "comment()" + indexInParent + toReturn;
              case AuthorNode.NODE_TYPE_PI:
                 // The token is PI.
                 toReturn = "/" + "processing-instruction()" + indexInParent + toReturn;
           } else {
              // This should not happen.
              toReturn ="";
           currentNode = (AuthorParentNode) currentNode.getParent();
     return toReturn;


Best regards,
Posts: 5217

Re: Copy XPath of an AuthorNode

Wed Oct 08, 2014 10:48 am


In Oxygen 16.1 which was just released we added the API AuthorDocumentController.getXPathExpression(int).

Radu Coravu
<oXygen/> XML Editor

Return to “Other Issues”

Who is online

Users browsing this forum: No registered users and 1 guest