Page 1 of 1

Copy XPath of an AuthorNode

Posted: Thu Jul 17, 2014 1:21 pm
by SSC
Hello,

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

Re: Copy XPath of an AuthorNode

Posted: Thu Jul 17, 2014 3:01 pm
by alex_jitianu
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) iter.next();
if (name.equals(child.getName())) {
index ++;
if (child == currentNode) {
indexInParent = "[" + index + "]";
break;
}
}
}
}
}

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

}
Best regards,
Alex

Re: Copy XPath of an AuthorNode

Posted: Wed Oct 08, 2014 10:48 am
by Radu
Hi,

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

Regards,
Radu