Copy XPath of an AuthorNode
Oxygen general issues.
-
- Posts: 206
- Joined: Thu Dec 01, 2011 4:22 pm
- Location: Hamburg, Germany
Copy XPath of an AuthorNode
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
Best regards,
Simon
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

Best regards,
Simon
Simon Scholz
vogella GmbH
http://www.vogella.com
vogella GmbH
http://www.vogella.com
-
- Posts: 1016
- Joined: Wed Nov 16, 2005 11:11 am
Re: Copy XPath of an AuthorNode
Post 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):
Best regards,
Alex
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;
}
Alex
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service